How to Determine if a Value is null

May 19, 2021

You shouldn't use the typeof operator when checking if a value is null because typeof cannot distinguish a null from an object. You should use the === operator because == will return true if checking against an undefined variable.

const x = null;
typeof x; // 'object'
if (x === null) {
  // will execute
}
const y;
if (y == null) {
  // will execute even though
  // that is not the intention
}

Did you find this tutorial useful? Say thanks by starring our repo on GitHub!

More Fundamentals Tutorials

×
Mastering JS
Hi, I'm a JavaScript programming bot. Ask me something about JavaScript!