What is the value of document.write(false == null)
. It should be true right (converted to same type before comparing - null is converted to false), if null is false then comparision should return true, but printing false. Why?

- 6,895
- 16
- 48
- 55
2 Answers
Your initial assumption is incorrect (as you may have worked out by the output!). ==
does indeed do type coercion, but there result is not necessarily what you expect. null
is an object, whose type is null - false
is an object whose type is boolean. There is no coercion under which objects of the null
and boolean
types can be equal, which is why this is false.
undefined
objects, on the other hand, can be coerced to null.
Note that the double-equals operator behaves in a bizarre way due to this - it's not even transitive. I would strongly suggest against its use unless somehow you know exactly how it will behave under your domain of inputs and you're sure you want this. It will almost certainly be better to coerce manually and use the ===
operator instead.

- 102,507
- 33
- 189
- 228
-
1As in Object Oriented Js Book, All values become true when converted to boolean, with the exception of the six falsy values: 1. "" 2. null 3. undefined 4. 0 5. NaN 6. false – minil Apr 14 '10 at 07:24
-
1If we're talking about specifications, check out http://interglacial.com/javascript_spec/a-11.html#a-11.9.3 (not the definitive ECMA copy, but that's a PDF so not hot-linkable). The rules for the equals operator show why your example returns false (it falls through to point 22). This doesn't change my general advice though, to use `===` instead in almost all situations. – Andrzej Doyle Apr 14 '10 at 07:32
-
@AndrzejDoyle Can you please tell why it falls through to point 22, rather than point 12: _If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false._ – Rob Lao Aug 14 '13 at 01:25
-
@BobL Because of point 1, which in this case is a `GOTO 14`. Point 12 is never evaluated, as points 2-13 are for cases where Type(x) === Type(y). – Andrzej Doyle Aug 14 '13 at 08:16
Edit: my original answer was completely incorrect....the below IS correct
(false == null) === false
(!null) === true
The 4th or 5th most popular answer in this post: Strangest language feature has a javascript truth comparison table.