Check for Undefined Value in JavaScript

We will understand how to check for undefined value in JavaScript in this article. Let us comprehend what an undefined value is.

Undefined

The "undefined" is a global property that represents a primitive type value. It is utilized to represent the absence of any value.

When a declared variable is not assigned any value then JavaScript shows an undefined value automatically.

In the above syntax, we have declared a variable called "str" but we have not assigned it a value. When we output the variable "str" then it will output undefined.

We can check undefined value by utilizing various methods which are given below:

  • Utilizing equality operator (==)
  • Utilizing identity operator (===)
  • Utilizing typeof operator
  • Utilizing the logical OR (||) operator
  • Utilizing the void operator

Let us comprehend each method one by one.

Utilizing equality operator (==)

The equality operator is used to perform type coercion and transforms the operands to the identical type before doing the comparison. After conversion into the same type then it checks the undefined value so we can check whether the value is undefined or not by utilizing the equality operator (==).

Demo:

Output:

We have checked the variable value utilizing the equality operator. We can witness in the output that the variable value is undefined.

Check for Undefined Value in JavaScript

Utilizing identity operator (===)

The identity operator does not perform type coercion like the equality operator but it checks both type and value equality so we can check whether the value is undefined or not by utilizing the identity operator (===).

Demo:

Output:

We have checked the variable value utilizing the identity operator. We can witness the output in which the variable value is undefined.

Check for Undefined Value in JavaScript

Utilizing typeof operator

The "undefined" is a data type so we can utilize typeof operator to check whether the value is undefined or not.

Demo:

Output:

We have checked the variable value utilizing the typeof operator. We can witness the output checks four variables. The first three variables are of string type and the fourth variable is undefined.

Check for Undefined Value in JavaScript

Utilizing logical OR (||) operator

The logical OR operator is utilized to give a default value to the variable when the variable is not declared. We can discover whether the value is undefined or not by utilizing the logical operator.

Demo:

Output:

We have checked the variable value utilizing the logical operator. We have defined the variable "var1" so the output displays its value and we have not declared the value of variable "var2" so the output displays undefined value.

Check for Undefined Value in JavaScript

Utilizing the void operator and undefined

The void operator is utilized to check the whether the value is undefined primitive value or not. We have to utilize "void(0)" or "void 0" which always gives back the undefined value so it is used to compare with the variable to find out the undefined value.

Code:

Output:

Here is the output in which we can witness that first three values are not undefined and the last value is undefined.

Check for Undefined Value in JavaScript

Conclusion:

We have understood how to check for undefined value in JavaScript. Some points to remember are as follows:

  • The variable which is announced but not assigned a value is called an undefined value.
  • There are many methods which are utilized to check the undefined value.
  • The following methods are used to check whether the variable is undefined or not:
    • Utilizing equality operator (==)
    • Utilizing identity operator (===)
    • Utilizing typeof operator
    • Utilizing the logical OR (||) operator
    • Utilizing the void operator





Latest Courses