Yes, they are identical, except for one case of document.all
(please see the first comment). We can see that in the ==
chart, null
and undefined
compares to true
only with itself and each other: null == null
is true
, and null == undefined
is true
, etc, and nothing else. So when we use something == null
, the only something
that can give a true
is null
and undefined
:

Even the TC39 is using it that way:
https://github.com/tc39/proposal-optional-chaining/blob/master/README.md
a?.b // undefined if `a` is null/undefined, `a.b` otherwise.
a == null ? undefined : a.b
However, I don't recommend using it in common daily coding. That's because, if the code a == null
is read by other programmers, they may immediately change it to a === null
and if the test cases are not able to catch it, then a subtle bug might be introduced in the system.
In an open source project where you have the ultimate decision for what goes in and what doesn't, then it can be used, with your code review and approval. Or, if you use it once or twice and then with proper comment up above to state what you are trying to do.
Otherwise, most other programmers may think if you really aim to compare just to null
and not considering for undefined
. If you write a === null || a === undefined
, then most everybody knows exactly what you are trying to do.
A third side effect is, if you say a == null
is identical to a === null || a === undefined
, you could attract tons of users on Stack Overflow, downvoting on you, saying don't ever use ==
in JavaScript.
An interesting fact is, even Babel translate a?.b
to compare with both null
and undefined
(void 0
), instead of just using == null
. Babel doesn't really have to, but maybe it does that so that when a programmer reads the compiled code, it is more clear.
But I don't know how many interviewers have asked me if the line if (a == null)
is good, and when I said we should use ===
, then I was viewed as just one step above greenhorn, because all the "pros" know that a == null
is for checking whether a
is null
or undefined
. So while maybe it is not good to actually use it in the code, it is good for testing if somebody is a pro.