This post will discuss how to check if a variable is null or undefined in JavaScript.

There are 7 falsy values in JavaScript – false, 0, 0n, '', null, undefined and NaN. A nullish value consists of either null or undefined. This post will provide several alternatives to check for nullish values in JavaScript.

 
To check for null variables, you can use a strict equality operator (===) to compare the variable with null. This is demonstrated below, where the boolean expression evaluates to true for only for null and evaluates to false for other falsy values.

Download  Run Code

 
Similarly, to check specifically for undefined variables, you can use the strict equality operator (===).

Download  Run Code

 
To check if a variable is either null or undefined, you can merge the above conditions. The following code demonstrates this, where the conditional statement is satisfied only for null or undefined values.

Download  Run Code

 
Finally, the standard way to check for null and undefined is to compare the variable with null or undefined using the equality operator (==). This would work since null == undefined is true in JavaScript.

Download  Run Code

That’s all about checking if a variable is null or undefined in JavaScript.