How to check for null in JavaScript

Because of a historical bug, typeof null in JavaScript returns “object” — so how do you check for null?

Dr. Derek Austin 🥳
Oct 8 · 5 min read
Photo by Ben Hershey on Unsplash

What is null?

“The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values.” — MDN Docs

This contrasts null from the similar primitive value undefined , which is an unintentional absence of any object value. For example, a variable that has been declared but not assigned any value is undefined, not null)

Unfortunately, typeof returns “object” when called on a null value, becayse of a historical bug in JavaScript that will never be fixed.

That means checking for null can’t be performed using typeof.

Photo by Eugene Triguba on Unsplash

null is falsy

null is a falsy value (i.e. it evaluates to false if coerced to a boolean)” — Josh Clanton at A Drip of JavaScript

Of course, that does not differentiate null from the other falsy values.

I explore using the == or === equality operators to do so next.

Photo by Nicholas Ruggeri on Unsplash

Falsy equality using ==

“Despite the fact that null is [falsy], it isn't considered loosely equal to any of the other falsy values in JavaScript. In fact, the only values that null is loosely equal to are undefined and itself.” —Josh Clanton at A Drip of JavaScript

As shown above, null is only loosely equal to itself and undefined, not to the other falsy values shown.

This can be useful for checking for the absence of value — null and undefined both indicate an absence of value, thus they are loosely equal (they have the same value even though they are different types).

So, when programming to check if a variable has any value at all before trying to process it, I can use == null to check for either null or undefined.

Photo by David Becker on Unsplash

Strict equality using ===

Generally, it is a good idea to catch both null and undefined values, so checking for null is one of the few times in JavaScript that using == is recommended, while otherwise === is generally recommended.

Photo by Dan Meyers on Unsplash

A real world example of when to check for null

“One way this error [‘null is not an object’] might occur in a real world example is if you try using a DOM element in your JavaScript before the element is loaded. That’s because the DOM API returns null for object references that are blank.” — Rollbar on the Top 10 JavaScript errors

The solution would be using an event listener that will notify us when the page is ready, and then running the script.

But still, it might be prudent to check if the DOM element is null before trying to access it.

Photo by Denys Nevozhai on Unsplash

Use typeof anyway with falsy power

“Thankfully since null isn’t really an object, it’s the only ‘object’ that is a falsy value, empty objects are truthy.” — Casey Morris in Daily JS

This can be conveniently checked using the logical not ! operator:

Photo by Dylan Freedom on Unsplash

Conclusion

The typeof keyword returns “object” for null, so that means a little bit more effort is required.

The value null is falsy, but empty objects are truthy, so typeof null === “object” && !null is a convenient way to check for null.

Or, comparisons can be made: null === null or null == undefined.

Go out there and check for null with confidence!

Photo by Puk Patrick on Unsplash

Additional resources

Photo by Toby Christopher on Unsplash

JavaScript in Plain English

Learn the web's most important programming language.

Dr. Derek Austin 🥳

Written by

😀 Full-Stack Web Developer 🤓 JavaScript 😄 React 😁 React Hooks 😆 Jest 🥰 CodeSandbox.io Contributor 🧠 Mobile App Developer 🤳 Doctor of Physical Therapy 🆒

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade