console.log( null === undefined )
An important rule of checking type with
undefined is that in the equality equation above, the result will be true only if both sides are either
This is helpful in checking against falsy values such as following -
let c; console.log(c == null); // true console.log(c == undefined); // true console.log(0 == null); // false console.log("" == null); // false
== should be rarely used. This is a good use case for when
== can be used. If you are unsure whether to use
Top comments (2)
The only thing I'd like to add is that this is the only case you should ever use
==for comparison, and always otherwise use
===. This is a good rule to follow especially if you're working without TypeScript or other type enforcement tool as it greatly reduces bugs caused by mixing datatypes.
Good point! Updating the post.