Thursday, September 4, 2025
HomeLanguagesJavascriptHow to check for “undefined” value in JavaScript ?

How to check for “undefined” value in JavaScript ?

In JavaScript, undefined’ is a primitive value that represents a variable that has been declared but has not been assigned a value, a function that has no return statement, or an object property that does not exist.

There are a few ways to check for ‘undefined’

Approach 1: Using the ‘typeof’ operator:

Syntax:

Javascript




// Declare a variable
let myVariable;
  
// Condition to check variable is defined or not
if (typeof myVariable === "undefined") {
    console.log("myVariable is undefined");
} else {
    console.log("myVariable is defined");
}


Here, ‘typeof’ operator returns the string ‘undefined’ if the variable has not been assigned a value.

Output:

myVariable is undefined

Approach 2: Comparing with the ‘undefined’ value:

Syntax:

Javascript




// Declare a variable
let myVariable;
  
// Condition to check variable is defined or not
if (myVariable === undefined) {
    console.log("myVariable is undefined");
} else {
    console.log("myVariable is defined");
}


Here, the ‘===’ operator checks if the value of the variable is exactly equal to ‘undefined’.

Output:

myVariable is undefined

Example 1:

Javascript




// Using the 'typeof' operator:
  
// Declare a variable
let fruit;
  
// Condition for check variable is defined or not
if (typeof fruit === "undefined") {
  console.log("fruit is undefined");
} else {
  console.log("fruit is defined");
}


Output:

fruit is undefined

Example 2:

Javascript




// Using the 'typeof' operator:
  
// Declare a variable and assign a value
// it will return fruit is defined
let fruit = "apple";
  
// Condition for check variable is defined or not
if (typeof fruit === "undefined") {
  console.log("fruit is undefined");
} else {
  console.log("fruit is defined");
}


Output:

fruit is defined

Example 3:

Javascript




// Comparing with the 'undefined' value:
  
// Declare a variable
let profile;
  
// Condition for check variable is defined or not
if (profile === undefined) {
  console.log("profile is undefined");
} else {
  console.log("profile is defined");
}


Output:

profile is undefined

Example 4:

Javascript




// Comparing with the 'undefined' value:
  
// Declare a variable and assign value
let profile = "neveropen";
  
// Condition for check variable is defined or not
if (profile === undefined) {
  console.log("profile is undefined");
} else {
  console.log("profile is defined as", profile);
}


Output:

profile is defined as neveropen
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6626 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11857 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS