The == algorithm (Abstract Equality Comparison Algorithm) isn’t something where you can simply assume an outcome unless you know the algorithm. You need to know the details of how it works.
For example, null and undefined are a special case. They do not do any type conversion other than to be considered equal to each other.
Otherwise there’s typically a type conversion that tries to reduce both operands to a common type. This often ends up being a toNumber conversion.
That’s why:
-
null == undefined; // true -
null == 0; // false -
+null == '0' // true
So if you know how the algorithm works, you know that undefined never equals anything except for undefined and null, but other types that are not strictly equal may be coerced down to types that are equal.
So doing if(!x) vs if(x==false) are entirely different tests.
-
if(!x)performs toBoolean conversion. -
if(x == false)uses a complex algorithm to decide the proper conversion.
So with…
if(x == false)
…if x is undefined, it is determined to not be equal to false, yet if x is 0 or even "0", it will be considered equal to false.
-
0 == false; // true -
"0" == false; // true