How to Check for Null in Javascript

Search for a command to run...

No comments yet. Be the first to comment.
Two ways to make pop-up images in a Google Sheet

Since last we spoke Iβve been recording several new long and short form videos. Check out the YouTube Channel if you havenβt. Iβve also wrapped up training for a marathon Iβll be racing on March 3rdβ¦ π€π€ Now to the sheetsβ¦ Do you ever feel lost in E...

Originally published for freeCodeCamp In this article I will show you how to allow for multiple items to be selected using the drop-down data validation feature in Google Sheets. Here's the Google Sheet we'll use for the example. You can make a copy...

Originally published for freeCodeCamp Without clean data, your spreadsheet is knocking on death's door. In this tutorial, I will show you two fast ways to clean up the data in your Excel or Google Sheets spreadsheet. When dealing with data sets, esp...

Originally published for freeCodeCamp In this article I will show you how to format cells in Microsoft Excel. We'll be looking particularly at the Accounting format for cells with numbers. At the end of the article I'll give you two bonuses: a short...

Sometimes you've gotta check to make sure that nothing isn't actually...nothing. π²ββ
In JavaScript, null is a primitive type intentionally containing the value of null. Undefined is a primitive type and represents a variable you declare without initiating a value.
So, null is nothing and undefined is just missing something. π€£

Not super helpful, I know. Let's dive deeper.
An example will help. Below we declare two variables. Let's keep it simple and use null and undefined to compare outcomes since they are sometimes confused because of their similarities.
//declaring two variables: one null and one undefined.
let leviticus = null;
// leviticus is null
let dune;
// dune is undefined
leviticus is intentionally without an object value (null). Whereas dune is declared, yet it's unintentionally missing a value (undefined).
You can check for null with the typeof() operator in JavaScript.
// typeof() will return 'object' when called on a null variable
console.log(typeof(leviticus))
// object
console.log(typeof(dune))
// undefined
Curiously, if you check with typeof(), a null variable will return object. This is because of a historic bug in JavaScript.
Another curiosity is that when you loosely check for equality using double equals ==, null and undefined will return true.
console.log(leviticus == dune)
// true
console.log(leviticus === dune)
// false
console.log(leviticus == null)
// true (but not as good a habit to use as strict equality shown in next example)
But when you strictly check for equality using triple equals ===, null and undefined will return false.
This is because null and undefined are both falsy in JavaScript. Falsy means that a value is considered false when encountered in a Boolean (true or false) context.
JavaScript uses coercion to coerce values from one type to another in order to be able to use them in a Boolean context.
But by strictly checking for equality, you can see that they are in fact not equal.
##How to to Check for Null with Strict Equality
The best way to check for null is to use strict and explicit equality:
console.log(leviticus === null)
// true
console.log(dune === null)
// false
Object.is() MethodAn equally foolproof way to check for null is to use the built-in Object.is() method:
console.log(Object.is(leviticus, null)
// true
console.log(Object.is(dune, null)
// false
null is a primitive type of a variable which evaluates falsy, has a typeof() of object, and is typically intentionally declared as nullundefined is a primitive type of a variable which evaluates falsy, has a typeof() of undefined, and represents a variable that is declared but missing an initial value.null == undefined evaluates as true because they are loosely equal.null === undefined evaluates as false because they are not, in fact, equal.<null_variable> === null is the best way to strictly check for null.Object.is(<null_variable>,null) is an equally reliable way to check for null.Take heart! As you've probably gathered, there are a plethora of brain teasers in the JavaScript ecosystem like this. But when you break it down, you can confidently understand them.

I hope this was a helpful breakdown for you. Keep coding, and keep leaning forward!
Come say hey to me over on Twitter: https://twitter.com/EamonnCottrell
Have a great one π.