Having posted answers to a few of these myself1, I find it difficult to write a canonical answer. We need to consider two cases:
- "What's wrong with JavaScript where
…==…
?". These might be closed as duplicates of a canonical question where the answer is Don't compare values of different types if you don't know what it means and Use ===
if you are not sure.
"How/Why is it that in JavaScript …==…
?". These are not so easy to answer, usually by hunting down the specific behaviour in the spec.
Finding an exact duplicate (with the same types) is very difficult. For example, Why does !!1==“1” equal true and !!2==“2” equal false? was erroneously closed as a dupe of truthiness in boolean to numbers comparison - twice! - even if the answers are quite different.
Creating a canonical answer for these types of questions is rather complicated due to the recursive nature of the abstract equality algorithm, and the number of possible types2 involved - 5 primitive types and various objects that behave differently in ToPrimitive
. Also it would mean to explain ToNumber
and ToString
in detail3, and after all the canonical answer won't be much more than a collection of quotes from the spec.
It might be a good idea though to maintain a collection of possible duplicates in an FAQ or this meta answer, so that they can be selected easily (and quickly, before answers are written) when a new question arises.
1: Sorry for making you jealous. Which doesn't mean I had the highest-voted answers, so I'll join your club :-)
2: Don't forget the extra comment on wrapper types and why they're different from primitives
3: Not counting the extra answer that explains the difference between ToBoolean
(!
, if
, ?:
) and comparing for equality with true
/false
.