Check if a Variable is Not NULL in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 2, 2024
4 min

banner

# Check if a Variable is Not NULL in JavaScript

Use the strict inequality (!==) operator to check if a variable is not null, e.g. myVar !== null.

The strict inequality operator will return true if the variable is not equal to null and false otherwise.

index.js
const a = 'hello'; // โœ… check if a variable is NOT null if (a !== null) { // ๐Ÿ‘‡๏ธ this runs console.log('the variable is NOT null'); } else { console.log('the variable is null'); } // ------------------------------------- // โœ… check if a variable is NOT null and undefined if (a != null) { // ๐Ÿ‘‡๏ธ this runs console.log('the variable is NOT null and undefined'); } else { console.log('The variable is null or undefined'); } // ------------------------------------- // โœ… check if a variable has been declared (without errors) if (typeof abcd !== 'undefined') { console.log('The variable has been declared'); } else { // ๐Ÿ‘‡๏ธ this runs console.log('The variable is undeclared or set to undefined'); }

check if variable is not null

The code for this article is available on GitHub

We used the strict inequality (!==) operator to check if the value stored in a variable is not equal to null.

The operator returns a boolean result:

  • true if the values are not equal
  • false if the values are equal
index.js
console.log(null !== null); // ๐Ÿ‘‰๏ธ false console.log('hello' !== null); // ๐Ÿ‘‰๏ธ true

The strict inequality (!==) operator considers two values of different types to be different, as opposed to the loose inequality (!=) operator.

This means that if you compare null with any other type, the strict inequality (!==) operator will always return false.

A very common mistake is to check if a value is truthy, instead of checking if it's not null. Here's an example.
index.js
const a = 'hello'; if (a) { console.log(`๐Ÿšจ a is NOT false, 0, empty string, null, undefined, NaN`); } else { console.log(`โ›”๏ธ๏ธ a is ONE OF false, 0, empty string, null, undefined, NaN`); }

In this example, we check if the value stored in the variable a is truthy. Truthy are all values that are not falsy.

The falsy values in JavaScript are: false, 0, "", null, undefined, NaN.

If the else block runs, the value of the variable is falsy and could be either of the 6 falsy values and not necessarily null.

Things could go wrong in many different ways when doing this. For example, if the value of a is equal to 0, the else block would run.

index.js
const a = 0; if (a) { console.log("โ›”๏ธ This doesn't run"); } else { console.log('โœ… This runs'); }

Even if this is what you want, it is better to be explicit.

index.js
const a = 0; if (a === 0) { console.log('โœ… This runs'); } else { console.log("โ›”๏ธ This doesn't run"); }

It's always recommended to explicitly use the strict operators (!==, ===) when making equality comparisons. They make your code more readable, explicit and less prone to errors.

One exception is when you need to check if a variable is not nullish.

# Check if a variable is NOT nullish in JavaScript

Here are 2 examples of checking if a variable is not nullish (null and undefined).

index.js
const a = 'hello'; // โœ… check if a variable is NOT null and undefined if (a != null) { // ๐Ÿ‘‡๏ธ this runs console.log('the variable is NOT null and undefined'); } else { console.log('The variable is null or undefined'); } // ------------------------------------- // โœ… check if a variable is NOT nullish (null and undefined) if (a !== null && a !== undefined) { // ๐Ÿ‘‡๏ธ this runs console.log('the variable is NOT null and undefined'); } else { console.log('The variable is null or undefined'); }

check if variable is not nullish

The code for this article is available on GitHub

The first example uses the loose inequality (!=) operator.

index.js
const a = 'hello'; if (a != null) { // ๐Ÿ‘‡๏ธ this runs console.log('the variable is NOT null and undefined'); } else { console.log('The variable is null or undefined'); }

The expression a != null checks if the variable a is not equal to both null and undefined.

When using the loose equality (==) operator, null is equal to undefined.

index.js
// ๐Ÿ‘‡๏ธ loose equality console.log(null == undefined); // ๐Ÿ‘‰๏ธ true // ๐Ÿ‘‡๏ธ strict equality console.log(null === undefined); // ๐Ÿ‘‰๏ธ false

An alternative approach to check if a variable is not nullish (null and undefined) is to be explicit and use the logical AND (&&) operator.

index.js
const a = 'hello'; if (a !== null && a !== undefined) { // ๐Ÿ‘‡๏ธ this runs console.log('the variable is NOT null and undefined'); } else { console.log('The variable is null or undefined'); }

We used the logical AND (&&) operator, so for the if block to run, both conditions have to be met.

The first condition checks if the variable is not equal to null and the second checks if the variable is not equal to undefined.

# Check if a variable has been declared in JavaScript

Use the typeof operator to check if a variable has been declared.

The typeof operator won't throw an error even if the variable has not been declared.

index.js
if (typeof abcd !== 'undefined') { console.log('The variable has been declared'); } else { // ๐Ÿ‘‡๏ธ this runs console.log('The variable is undeclared or set to undefined'); }

check if variable has been declared

The code for this article is available on GitHub

Notice that the abcd variable has not been declared, but no error occurs when running the code sample.

The typeof operator returns a string that indicates the type of a value.

The operator won't cause an error even if the variable you are checking for has not been declared.

Once you find that the variable has been declared, you can check if the variable is not null.

index.js
if (typeof abcd !== 'undefined') { console.log('The variable has been declared'); if (abc !== null) { console.log('The variable is not null'); } else { console.log('The variable is null'); } } else { // ๐Ÿ‘‡๏ธ this runs console.log('The variable is undeclared or set to undefined'); }

Once we enter the if block, we know that the variable has been declared and we can safely access it.

The inner if statement checks if the variable is not null.

Which approach you pick is a matter of personal preference. I always use the direct not null check - if (a !== null) {}.

In my experience, it's better to be explicit even if it makes your code more verbose.

Getting an error is not necessarily a bad thing because most of the time, it surfaces a bug in your application.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev