null == undefined
does indeed evaluate to true, but null === undefined
evaluates to false.
The difference in those two statements is the equality operator. Double-equal in Javascript will convert the two items to the same type before comparing them; for null == undefined
, this means that the null
is converted to an undefined variable before the comparison is done, hence the equality.
We can demonstrate the same effect with strings and integers: "12" == 12
is true, but "12" === 12
is false.
This example gives us an easier way to discuss your next point, about adding one to each of them. In the example above, adding 1 to the integer obviously gives 13
, but with the string "12" + 1
gives us a string "121"
. This makes perfect sense, and you wouldn't want it any other way, but with a double-equal operator, the original two values were reported as equal.
The lesson here is to always use the triple-equal operator in preference to the double-equal, unless you have a specific need to compare variables of different types.
Your final point demonstrates the fickle nature of null
in general. It is a peculiar beast, as anyone who's ever tried to work with a nullable database field will tell you. Null has a very specific definition in computer science, which is implemented in a similar way across multiple languages, so the situation you describe is not a special Javascript weirdness. Null is weird. Don't expect it to behave like an alternative name for false
, because it doesn't work that way. The built-in infinity
value can behave in a similarly bizarre way, and for similar reasons.
Javascript does have its share of weirdness though. You might be interested in reading http://wtfjs.com/, which has entries for a whole load of strange things that Javascript does. Quite a few of them are to do with null
and undefined
(did you know it's actually possible to redefine the value of the built-in undefined
object?!), and most of them come with an explanation as to what's actually happening and why. It might be helpful in showing you why things work the way they do, and will definitely helpful in showing you things to avoid! And if nothing else, it makes for a good entertaining read to see some of the abuses people have tried throwing at the poor language.