JavaScript has different equality comparison operators
- Equal
== - Strict equal
===
It also has a logical NOT ! and I’ve tended to think of using a double logical NOT, !!x, as basically the same as true == x.
However I know this is not always the case, e.g. x = [] because [] is truthy for ! but falsy for ==.
So, for which xs would (true == x) === !!x give false? Alternatively, what is falsy by == but not !! (or vice versa)?
Any
xwhere its Boolean conversion is not the same as its conversion by the Abstract Equality Comparison Algorithm.An example is a string with only whitespace:
Its Boolean conversion is
true(as is the case with any non-empty string), but its==comparison isfalsebecause a string with only white space will be converted to the number0, and thetruevalue will be converted to the number1, and those values are not equal.or to show the ultimate values the
==is comparing:and to show the result of
!!x, it would be equivalent to this:So your original expression could crudely be seen as the following:
I say “crudely” because this certainly doesn’t capture all the detail of the algorithm linked above, and won’t be the same for all values provided to the operands.
To understand how
==treats its operands, you really need to study the algorithm a bit.