How to check if a Variable is Not NULL in JavaScript

Check if a Variable is Not NULL in JavaScript

In order to check if a variable is not null, you can use the strict inequality operator (!==). To use this operator, simply place it between the variable and null, like so: myVar !== null. This operator will return true if the variable is not equal to null and false otherwise.

Example –

const a = 'Hello';

if (a !== null) {
  console.log('This a is NOT null');
} else {
  console.log('This a is null');
}
Check if a Variable is Not NULL in JavaScript using inequality operator (!==).
Check if a Variable is Not NULL in JavaScript using inequality operator (!==).

Explanation –

The Strict inequality (!==) operator returns a boolean result:

  1. true if the values are not equal
  2. false if the values are equal
console.log(null !== null); 
console.log('hello' !== null);
Checking two diffrent value with strict inequality (!==) operator.
Checking two diffrent value with strict inequality (!==) operator.

The strict inequality (!==) operator considers two values to be different if they are of different types, as opposed to the loose inequality (!=) operator which does not consider type when determining if two values are different.
This means that the strict inequality (!==) operator will always return false if you compare null with any other type.

A lot of people make the mistake of checking if a value is truthy instead of checking if it’s not null. Here’s an example.

const a = 'hello';

if (a) {
 console.log(`This a is NOT false, 0, empty string, null, undefined, NaN`);
} else {
 console.log(`This a is ONE OF false, 0, empty string, null, undefined, NaN`);
}
Checking if a value is truthy instead of checking if it's not null.
Checking if a value is truthy instead of checking if it’s not null.

We check if the value stored in the variable a is truthy. Truthy values are those that are not falsy.
The values in JavaScript that are not true are: false, 0, "", null, undefined, NaN.

ℹ️ If the else block runs, the value of a is falsy and could be any of the 6 falsy values, not just null.

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

const a = 0;

if (a) {
  console.log(" This block doesn't run");
} else {
  console.log(' This block runs');
}
If the value of a is equal to 0, the else block would run.
If the value of a is equal to 0, the else block would run.

You should never write code that is confusing and indirect. You should always be more explicit.

const a = 0;

if (a === 0) {
  console.log('This block runs');
} else {
  console.log("This block doesn't run");
}
Writing more explicit code.
Writing more explicit code.

ℹ️ You should always use the strict operators (!==, ===) when comparing values, as this makes your code more readable and less likely to contain errors.
Note – The following values are not truthy in JavaScript:

  • null
  • undefined
  • 0
  • "" (the empty string)
  • false
  • NaN
How to check if a Variable is Not NULL in JavaScript