Deep Tech Point
first stop in your tech adventure
How to check if a variable is not null in JavaScript?
February 11, 2021 | Javascript

What is null in Javascript?

In JavaScript the value null represents the intentional absence of any object value. The value null is one of JavaScript’s primitive values and is treated as falsy for boolean operations. We could compare a value null to a similar primitive value – undefined. Undefined is an unintentional absence of any object value, and this “unintentional” is the thing that differentiates null from the undefined.

In JavaScript null is “nothing” – it is supposed to be something that doesn’t exist. The data type of null is an object — it is usually set on purpose to indicate that a variable has been declared but not yet assigned any value.

There are a few ways to check if a variable IS null.

Do not use typeof
First of all, we should mention that typeof returns “object” when called on a null value. This is a historical bug in JavaScript and you should not expect that to be fixed. Consequently, you cannot use typeof when checking if a variable is null.

Null is falsy
As already mentioned, null is a falsy value, meaning it is treated as false for boolean operation, so one of the simplest ways to check if a variable is null is to know that logic – that null evaluates to false in conditionals or if coerced to a boolean value. However, there is a problem with this method – it doesn’t differentiate the value null from the other falsy values, such as 0, 0n, undefined, false, NaN, and the empty string “”. All these are falsy values coerced by JavaScript’s typing engine into a boolean value, however, they are not necessarily equal to each other. So, this method is basically not the solution either.

You could use equality operators, such as == and ===
A simple approach to checking for value null in JavaScript is to check if a value is loosely equal to null using the double equality == operator. Of course, there are advantages and disadvantages to this method: value null is only loosely equal to itself and undefined, not to the other falsy values shown, so using double equality narrows falsy values. This method could be useful when you want to check if a variable has any value at all because both null and undefined indicate an absence of a value (they have the same value) but they are both different types.
However, if you want to exclude any undefined values and make sure our value is exactly null, we must use strict or triple equality === operator. A strict equality operator is much wordier comparing to double equality, but those who will read your code will obviously know that with this method both null and undefined values are being checked for.

So, in the end, how do you check if a variable IS NOT NULL?

Let’s go straight to the point:

if (myVariable !== null) { code }

The code will be executed if myVariable is not null. Or the code inside our if(myVariable !== null) { code } will NOT be executed only when myVar is equal to null. Exactly what you’re looking for.
The code will be executed if myVariable is undefined or false or 0 or NaN or any other.