Register Login

How to check if a variable is undefined or not?

Updated Sep 06, 2022

JavaScript has a primitive data type called undefined that represents a variable without being assigned. In addition to undefined property, JavaScript has a null value, and both values represent empty values in JavaScript.

These often confuse users about whether a variable is undefined or null. In this article, we will discuss how undefined works in JavaScript and how to check whether a variable is undefined or not.

What is Undefined in JavaScript?

Users can use the primitive type undefined property to check whether a variable has a value assigned to it or not. JavaScript interpreter returns undefined when users access a variable or object property that they have not yet initialized.

When a variable is declared or initialized, but users do not assign any value to it, the interpreter automatically displays "undefined."

Let us see an example of how undefined works:

function demo(d) {
  if (d === undefined) {
    return 'This is an Undefined value.';
  }
  return d;
}
let a;
console.log (demo(a))

Output:

Run Code

Explanation:

Here, we have used the function "demo" and declared a parameter "d" with the value undefined. We then call the function, and it returns the statement of the “if block”.

Return Value:

The undefined returns 'defined' if users assign a variable with any value and 'undefined' if not.

How to check whether a variable is undefined?

Users can check whether a variable is undefined or not by using two techniques. These are using the typeof operator and comparison operators, the loose equality operator == or strict equality operator ===.

Method 1: Using typeof operator to check undefined variables:

It is an operator used for checking the type of variable defined within it. Users can use this typeof operator with any operand, such as any function, variable, or object whose type they want to find out operating the typeof operator.

One most important reason to use typeof is that it does not throw any error if users do not declare a variable with any value.

Let us see how typeof operator works:

let a;
if (typeof a === 'undefined') {
  console.log('Variable is undefined');
}

Output:

Run Code

Explanation:

The typeof operator defines 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.

Another example of the typeof operator is as follows:

const a = []
const b = 5
let c;
typeof list //"object"
typeof count //"number"
typeof "test" //"string"
typeof color
console.log (typeof a)
console.log (typeof b)
console.log (typeof c)

Output:

Run Code

Method 2: Using the comparison operators to check undefined:

Another technique to check undefined is to use comparison operators in JavaScript. These are Loose equality operators (==) and strict equality operators (===). But users should use the Strict Equality Operator (===) instead of the Loose Equality Operator (==) because of 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.

Let us see the code snippet and understand how it checks variables:

let obj = {
    a: [],
    b: 20
};
if (obj.c === undefined) {
    console.log("This is undefined");
}

Output:

Run Code

Explanation:

Here, the comparison operator checks and finds out whether the variable has any value assigned to it or not. Since the variable c has no value assigned to it, the operator returns undefined.

Another example of a Strict Equality Operator is as follows:

let demo = [12, 34, 66, 78];
if (demo[40] === undefined) {
    console.log("This is undefined");
}

Output:

Run Code

Note:

The highlighted difference between the two approaches, i.e., typeof operator and comparison operator, is that if the user does not declare x, the Strict Equality (===) for checking undefined variables throws a ReferenceError, whereas the typeof does not.

Conclusion:

We hope this article has catered to brief details on how to check whether a variable is undefined or not. Both approaches work accurately, but users should choose the most efficient method.


×