Register Login

Check if the Variable is undefined or null

Updated Aug 16, 2022

In JavaScript, undefined and null are both falsy values, which indicate no value or absence of values within a variable. Users can check whether they are dealing with the undefined or null variable using the OR operator (||) with == and === operators. Also, users can use the typeof operator to check undefined and null variables. This article discusses the methods that help check null or undefined values in a variable.    

What does "undefined" mean in JavaScript?

When a user does not assign a value to a variable, it is of type undefined. The undefined type is a primitive type that contains only a single value, i.e., undefined. When users declare a variable but do not initialize it, i.e., it means the variable has a value assigned to it, i.e., undefined.

What is null in JavaScript?

JavaScript null is an empty or unknown, or missing value. The value null indicates the intended absence of an object value. Like the undefined, the null is also a primitive value of JavaScript, and users can treat it as falsy for Boolean operations.

How to check whether a variable is undefined or null?

Method 1: Using the OR (||) operator to check undefined and null variable

As discussed above, users can check if a variable is null or undefined using the OR (||) operator. It checks if the variable satisfies either of the two conditions. When users use it with two Boolean values, the OR operator will return a "true" value if it considers both conditions true. Also, using the typeof operator can help differentiate between undefined and null values.

There are two choices to check whether a variable is undefined or null.

These are as below:

The loose equality operator (==) and the strict equality operator (===) have different functioning. The Loose equality operator (==) considers null and undefined as the same. On the other hand, the Strict operator (===) checks and returns that the "undefined" and "null" are not the same.

Code Snippet:

let x = undefined
let y = null
console.log(x == y); 
console.log(x === y);

Output:

Run Code

In JavaScript, users can use the equality operator (==) to check whether a variable is undefined or null.

For example:

let a;
if (a == null) {
  console.log('The variable is a null valued');
} else {
  console.log(a);
}

Output:

Run Code

Again, if we consider the Strict Equality Operator (===) and check a given variable, it will return "undefined".

Code Snippet:

let a;
if (a === null) {
  console.log('The variable is a null valued');
} else {
  console.log(a);
}

Output:

Run Code

Let us see how the OR operator checks an undefined or null variable:

Code Snippet:

const a = null;
// Here, we are checking whether the variable is undefined or null
if (a === undefined || a === null) {
  console.log('Variable is null or undefined');
} else {
  console.log('Variable is NOT null or undefined');
}

// Here, we are checking whether the variable is NOT undefined or null
if (a !== undefined && a !== null) {
  console.log('Variable is NOT null or undefined');
}

Output:

Run Code

Explanation:

In the above example, we have used the first if statement to check if the "a" variable is either undefined or null. If it satisfies either of the conditions, then the if block will execute; otherwise, the else block will execute.

In the second statement, we have used the if statement to check if the variable (a) is NOT equal to undefined and is not equal to null. Lastly, we have used the logical AND (&&) operator to define that the variable should meet both conditions to execute the if block.

Method 2: Using the typeof operator to check undefined and null variable:

In addition to the OR operator, users can also use the JavaScript typeof operator. It checks the type of variable defined within it.

Code Snippet:

let a;
if (typeof a === 'undefined') { 
console.log('The variable is undefined ');
} 
else if (typeof a === 'null') { 
console.log('The variable is Null-value');
}

Output:

Run Code

Explanation:

The typeof operator specifies the type of the variable. However, users should note that if they chuck in a non-existing reference variable, the typeof operator will work with it, returning it as undefined.

Note: It is the right choice to check whether a variable is undefined or null with OR (||) operator.

Differences between undefined and null:

Undefined Null
In JavaScript, when users declare a variable but do not assign a value within it, the JS interpreter automatically returns the variable as undefined. Therefore, as shown in the above code sections, if users try to display the value of such a variable, it will return "undefined," which indicates no value is assigned. Users can assign the null to a variable to represent that the variable has no value and is a special assignment value.
Undefined implies the absence of a variable itself. Null implies the absence of a value within a variable.
The JS interpreter converts the undefined to NaN while executing primitive operations. The JS interpreter converts the null to zero (0) while executing primitive operations.
Undefined is a type itself. A null is an object itself

Conclusion:

We hope this article has catered to the clear concept of checking whether a variable is undefined or null. We have learned how to check in JavaScript if a variable is undefined, null, or nil, using the “==”,“===” and typeof operators, stating some minor advantages and disadvantages of each method.


×