How to Check For Null Values in JavaScript

Here are four ways to check for null values in JavaScript:

  1. Using strict equality operator(===)
  2. Using loose equality operator(==)
  3. Using Object.is()” method
  4. Using typeof operator

Method 1: Using the strict equality operator

The strict equality operator checks value and type, ensuring the variable is null and not another false value (e.g., undefined, false, 0, or an empty string).

Here is a syntax:

if (variable === null) {
  // variable is null
} else {
  // variable is not null
}

This method is the most accurate way to check for null values since it checks both value and type, ensuring that the variable is null and not another false value (e.g., undefined, false, 0, or an empty string).

Here is a code example:

const nullValue = null;
const undefinedValue = undefined;
const emptyString = '';

if (nullValue === null) {
  console.log('nullValue is null');
} else {
  console.log('nullValue is not null');
}

if (undefinedValue === null) {
  console.log('undefinedValue is null');
} else {
  console.log('undefinedValue is not null');
}

if (emptyString === null) {
  console.log('emptyString is null');
} else {
  console.log('emptyString is not null');
}

Output

nullValue is null
undefinedValue is not null
emptyString is not null

In this example, the strict equality operator (===) function checks if nullValue, undefinedValue, and emptyString are null.

Since they have different values and types, it returns true only for nullValue and false for the other two variables.

The strict equality operator (===) for precise comparisons is recommended.

Method 2: Using the loose equality(==) operator

You can use the loose equality operator to check for null values. This can be useful when checking if a variable is either null or undefined.

Here is a code example:

let data = null;

console.log(data == null);

Output

true

Using == with null in this manner will catch null and undefined values. However, you should be careful when using the loose equality operator, as it can lead to unexpected results due to type coercion.

console.log("0" == 0)
console.log(false == 0)
console.log(true == 1)

Output

true
true
true

Because of these quirks, many JavaScript style guides and linters recommend always using the strict equality operator (===) to avoid unexpected type coercion. But as long as you know the nuances and use it intentionally, loose equality can be a tool in your belt.

Method 3: Using Object.is() method

Object.is() is an ES6 method that determines whether two values are the same. This works like the strict equality operator.

Here is a code example:

let data = null;
let newData;

console.log(Object.is(data, null));
console.log(Object.is(newData, null));
console.log(Object.is(data, undefined));
console.log(Object.is(newData, undefined));
console.log(Object.is(data, newData));
console.log(Object.is(null, undefined));

Output

true
false
false
true
false
false

Method 4: Using typeof operator

You can also use the combination of typeof and logical not(!) operators to check for null values in JavaScript.

Here is a code example:

let data = null;
console.log(typeof data === "object" && !data);

const isNull = (value) => typeof value === "object" && !value
console.log(isNull(data))

Output

true
true

That’s it!