How to check if a variable is undefined or null in JavaScript

How to check if a variable is undefined or null in JavaScript:

In this post, we will learn how to check for undefined or null variable in JavaScript. If we create one variable and don’t assign any value to it, JavaScript assigns undefined automatically. null is an assignment value, we need to assign a variable null to make it null.

If you don’t know the content of a variable in JavaScript, it is always a good idea to check if it is undefined or null.

Before going to try different options to check for undefined and null, let’s check how equality operator(==) and identity operator(===) behaves with undefined and null.

== and === :

Let’s take a look at the below program:

let nullVar = null;
let undefinedVar = undefined;
let uninitializedVar;

console.log(`nullVar ${nullVar}`);
console.log(`undefinedVar ${undefinedVar}`);
console.log(`uninitializedVar ${uninitializedVar}`);

console.log('\nUsing == :')
console.log(`nullVar == null ${nullVar == null}`)
console.log(`nullVar == undefined ${nullVar == undefined}`)
console.log(`undefinedVar == null ${undefinedVar == null}`)
console.log(`undefinedVar == undefined ${undefinedVar == undefined}`)

console.log('\nUsing === :')
console.log(`nullVar === null ${nullVar === null}`)
console.log(`nullVar === undefined ${nullVar === undefined}`)
console.log(`undefinedVar === null ${undefinedVar === null}`)
console.log(`undefinedVar === undefined ${undefinedVar === undefined}`)

Here, we have created two variables nullVar and undefinedVar with null and undefined values. Then we are trying == and === with null and undefined. If you run this program, it will print the below output:

nullVar null
undefinedVar undefined
uninitializedVar undefined

Using == :
nullVar == null true
nullVar == undefined true
undefinedVar == null true
undefinedVar == undefined true

Using === :
nullVar === null true
nullVar === undefined false
undefinedVar === null false
undefinedVar === undefined true

As you can see here, for ==, it returns true for both null and undefined. So, we can use it for checking undefined or null.

So, we can just compare a variable with null by using == to check if it is undefined or null.

const isNullOrUndefined = (e) => {
  return e == null;
};

let firstVar = 1;
let secondVar = null;
let thirdVar = undefined;

if (isNullOrUndefined(firstVar)) {
  console.log("firstVar is undefined or null");
}

if (isNullOrUndefined(secondVar)) {
  console.log("secondVar is undefined or null");
}

if (isNullOrUndefined(thirdVar)) {
  console.log("thirdVar is undefined or null");
}

In this program, isNullOrUndefined method checks if a variable is undefined or null. It returns one boolean value. true if it is undefined or null and false otherwise.

Method 2: By using typeof:

Let’s take a look at typeof on how it works:

console.log(typeof undefined);
console.log(typeof null);

It will print:

undefined
object

So, typeof undefined returns undefined. We can use typeof to check for undefined. For null, we can use ===.

const isNullOrUndefined = (e) => {
  return e == null || typeof e === 'undefined';
};

let firstVar = 1;
let secondVar = null;
let thirdVar = undefined;

if (isNullOrUndefined(firstVar)) {
  console.log("firstVar is undefined or null");
}

if (isNullOrUndefined(secondVar)) {
  console.log("secondVar is undefined or null");
}

if (isNullOrUndefined(thirdVar)) {
  console.log("thirdVar is undefined or null");
}

You might also like: