Check for Undefined Value in JavaScript

In this tutorial, we will explore the process of verifying undefined values in JavaScript. It is essential to grasp the concept of an undefined value before proceeding.

Undefined

The global property known as "undefined" represents a primitive value type, serving the purpose of indicating the lack of a value.

In JavaScript, if a variable is declared without being assigned a value, it will automatically have the value of undefined.

Example

let str;
console.log(str);

Within the syntax presented earlier, a variable named "str" was defined without being initialized with a value. Consequently, attempting to output the variable "str" will result in the display of "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 facilitates type coercion by converting operands to the same type for comparison. It first ensures both operands are of the same type before checking for equality. This process includes checking for undefined values, allowing us to determine if a value is undefined using the equality operator (==).

Demo:

Example

function check(var1) {
  if (var1 == undefined) {
    return 'The value is undefined.';
  }
  return var1;
}

let var2;

console.log(check(var2));

Output:

The variable value has been inspected using the equality operator, revealing that it is indeed undefined in the output.

Utilizing identity operator (===)

The identity operator differs from the equality operator as it does not coerce types. It verifies both type and value equality. By using the identity operator (===), we can determine if a value is undefined.

Demo:

Example

function check(str1) {
  if (str1 === undefined) {
    return 'It is an undefined value.';
  }
  return str1;
}

let str2;

console.log(check(str2));

Output:

The variable value has been inspected using the identity operator to determine that it is undefined. The output clearly shows that the variable value is not defined.

Utilizing typeof operator

The data type "undefined" allows us to employ the typeof operator to verify if a value is undefined or not.

Demo:

Example

function check(x) {

    return typeof(x);
}

let product = {
    name: "iphone 15 Pro",
    color: "White Titanium",
    size: "1 TB",
};
console.log(check(product.name));
console.log(check(product.color));
console.log(check(product.size));
console.log(check(product.price));

Output:

The type of each variable was examined using the typeof operator. The output indicates the types of four variables. The initial three variables are classified as strings, while the fourth variable is classified as undefined.

Utilizing logical OR (||) operator

The logical OR operator is employed to assign a default value to a variable in cases where the variable has not been defined. It helps us determine if a value is undefined or not by using this logical operator.

Demo:

Example

let var1 = 2;
let result1 = var1 || "default value, i.e., the undefined value";
console.log(result1);

let var2;
let result2 = var2 || "default value, i.e., the undefined value";
console.log(result2);

Output:

The variable value has been inspected using a logical operator. The variable "var1" has been initialized to show its value, while the variable "var2" has not been assigned a value, resulting in an output of undefined.

Utilizing the void operator and undefined

The void operator is commonly used to determine if a value is an undefined primitive value. By using expressions like "void(0)" or "void 0", we can consistently obtain the undefined value. This technique is helpful when comparing variables to identify undefined values.

Code:

Example

function check(y) {
    var val1 = void(0)
    var val2 = typeof(y)
    
    return String(val1) === val2
 }

let person = {
    name: "Naina",
    age: 24,
    weight: 55
};
console.log(check(person.name)?"undefined":"not undefined");
console.log(check(person.age)?"undefined":"not undefined");
console.log(check(person.weight)?"undefined":"not undefined");
console.log(check(person.address)?"undefined":"not undefined");

Output:

The following output demonstrates that the initial three values are defined, while the final value is undefined.

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
  • Utilizing equality operator (==)
  • Utilizing identity operator (===)
  • Utilizing typeof operator
  • Utilizing the logical OR (||) operator
  • Utilizing the void operator

Input Required

This code uses input(). Please provide values below: