What does the !! operator mean in a Return

Asked

Viewed 26 times

0

I know the exclamation (!) is the denial of my expression, but in my function has two exclamations, example:

public get isChecarAlgo() {
    return !!this.algumValorNumerico;
}

In that case, what does it mean? That the return will be true or false depending on whether or not it has value?

  • 1

    It is a dirty trick to convert a value or absence of this into boolean. The two exclamations serve to deny denial. So if it has value the result will be true and otherwise it will be false.

  • I figured... thank you so much for confirming!

  • This "truqe" is used when the value is Truthy or falsy to make a quick conversion. Similar to what some miniseries use to decrease where explicitly true and false is written, like let x = true, y=false; for that reason let x=!0, y=!1;

  • Just to let you know that this is a Javascript feature - as Typescript is a superset of the JS, ends up having this "by table".

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.