How to Check if the Variable is Undefined

It is possible to check whether a variable is defined or not in JavaScript with the typeof operator which returns a string telling the type of the operand.

Do the following to check if the value's type is "undefined":

Javascript the value's type is "undefined"
let myVar; if (typeof myVar === 'undefined') { console.log('Variable is undefined'); }

To check if a variable is undefined, you can use comparison operators — the equality operator == or strict equality operator === .

If you declare a variable but not assign a value, it will return undefined automatically. Thus, if you try to display the value of such variable, the word "undefined" will be displayed.

You can also check The Difference Between Null and Undefined in JavaScript snippet.

undefined vs “undefined”

There are two ways of determining whether a variable is not defined either by value or by type. If you check by value, you will get that variable is assigned a value or not. In the case of undefined, the assigned variable don’t have any value but the variable exists.

Checking the type is done with the typeof operator. In the case of variable === “undefined”, the type of variable is undefined.