2 ways to check if a variable exists or defined in JavaScript or not

How to check if a variable exists or defined in JavaScript or not:

In this post, I will show you how to check if a variable in JavaScript is defined or not. We can use an if block or typeof operator to do that.

Method 1: By using typeof:

The typeof operator determines the type of a variable. If we use a variable var1 with typeof, we have to use it as like typeof var1. It will return any of the following type:

  • number, boolean, symbol, string, undefined, symbol, object.

The advantage of typeof is that it will not throw any ReferenceError even if the variable we are using is not found.

So, we can compare the result of typeof with undefined string to determine if a variable is defined or not. Let me show you an example with typeof:


if(typeof var1 === 'undefined'){
    console.log('var1 is undefined');
}else{
    console.log('var1 is defined');
}

let var2;
if(typeof var2 === 'undefined'){
    console.log('var2 is undefined');
}else{
    console.log('var2 is defined');
}

let var3 = 10;
if(typeof var3 === 'undefined'){
    console.log('var3 is undefined');
}else{
    console.log('var3 is defined');
}

It will print:

var1 is undefined
var2 is undefined
var3 is defined

In this example,

  • var1 is undefined. This is not defined at any place in this program. So, typeof will return undefined for it.
  • var2 is defined but it is not initialized. typeof returns undefined for this as well.
  • The last variable, var3 is defined and also it is initialized. typeof returns the type of the variable, i.e. number for this example. So, it will move to the else block and print it as defined.

typeof with null variables:

If you use typeof to check for a null variable, it will return object. So, we can’t use typeof to check if an element is null or not.

To check for null variables, we can use an if block.

let var1 = null

if(var1 === null){
    console.log('var1 is null');
}else{
    console.log('var1 is not null');
}

Method 2: By using if:

We can also use the if statement with a variable. if statement checks for other types as well. It will check for undefined, false, NaN, empty string, null, and 0. For all of these, it will be false.

Let’s try it with different types of data:

let varArray = [0, false, NaN, undefined, '', null, 'hello', 20, -20, 'a']

varArray.forEach(item => {
    if(item){
        console.log(`${item} => true`);
    }else{
        console.log(`${item} => false`);
    }
})

In this example, we created an array varArray and by using a for loop, we are checking the result of if for each of the items in varArray.

It will print:

0 => false
false => false
NaN => false
undefined => false
 => false
null => false
hello => true
20 => true
-20 => true
a => true

javascript check variable exists or not

Try-catch:

If we try to access an undefined variable, it will throw a ReferenceError. We can use a try-catch block and if any exception raised, it can be handled in the catch block.

try{
    console.log(var1);
    console.log('var1 is defined');
}catch(e){
    console.log('var1 is not defined');
}

It will print var1 is not defined.

You might also like: