The == operator does not just check if things are false; it follows a procedure called the Abstract Equality Comparison Algorithm, which can be seen here: the ECMAScript specification .
undefined == 0 and undefined == '' :
According to the specification, undefined compares true only with undefined or null . Since neither 0 nor '' are none of these values, it returns false.
null == 0
According to the specification, null compares only the truth with another null or with undefined - here and not here.
Use ===
Generally, it's better to use === and do the comparison that you really intend, instead of trusting the == algorithm, and usually faster. It is easy to be caught, as you saw when the algorithm does not do what you expect, and it is usually easier to be explicit than to keep track of which path the comparison algorithm will follow.
For general "sameness" in javascript, see this MDN page .
source share