A common misconception
"...If double equalto (==
) operator only checks/compares for values & not for types..."
That's an incorrect assumption, though it's often repeated by people. In reality, the ==
does check types, and in fact pays far more attention to the types than a ===
comparison does.
See Abstract Equality Comparison Algorithm.
A ==
comparison doesn't do a simple toBoolean conversion. Rather it walks through a somewhat complex recursive algorithm, which, after checking the types, attempts to coerce the operands to the same type if they don't match.
The type coercion that it performs is very specific to the types of the operands. A different sequence of coercions can take place for different type pairs. Usually (but not always) it ends up ultimately coercing the operands down to number
types.
Why !
ing the operands changes things
When you manually coerce both operands using !
, you're now doing a simple toBoolean conversion causing the types to match, which avoids the type coercive part of the algorithm, making it behave essentially like the Strict Equality Comparison Algorithm.
So the only way to predict the outcome of a ==
comparison when the types don't match is to understand that Abstract algorithm.
Don't forget about NaN
And FYI, there's one more "falsey" value to consider, NaN
. Its ==
comparison will always be false
, no matter what. Even when comparing to another NaN
value, it'll be false
.