0

Is bol === !0 exactly the same as bol == true?

Example:

function myFunction(bol){
    if(bol===!0){
        return 1;
    }else{
        return -1;
    }
}

alert(myFunction(true));//1

Will I ever run into problems if I decide to use it? (different than code readability)

mithril333221
  • 799
  • 2
  • 9
  • 20

1 Answers1

3

No. 1 == true is true but 1 === !0 is false.

Since !0 is true, this is equivalent to bol === true. Note how you should still use the three equal signs to get exactly the same behavior.

The === means JavaScript will not try to coerce the values when comparing. If you actually want to coerce, you would use ==. However, in this case, the if will just coerce for you:

if (bol) { ... }

is basically the same as

if (bol == true) { ... }
Tikhon Jelvis
  • 67,485
  • 18
  • 177
  • 214