When we see some code from a company, such as from Google or from the ECMA committee:
a == null, what does it mean?
It may seem it is for checking whether the variable refers to the primitive
null, but in fact, it means:
a === null || a === undefined
In fact, we can see that
a == null is identical to using
(a === null || a === undefined), not more, not less. They are exactly the same.
This is used by the ECMA TC39 (Ecma International, Technical Committee 39), when they defined optional chaining:
which they used
(a == null) ? undefined : a.b
a == null is exactly: when
undefined, then simply return
undefined. While we may not use it if other people reading our code may get confused, it is good when we read
a == null and know what it means.
The only exception to the rule above is
document.all == null // true
by the rule above, it may appear then
document.all === null || document.all === undefined
true. But it returns
false. This is the only known exception, but
document.all returns an object, while
Reference on MDN.