How to check if undefined in JavaScript?

   JavaScript

If you would like to check if undefined in JavaScript or the variable hasn’t been declared or the variable has the value undefined, then using ‘typeof‘ operator, you can validate the type of an undeclared variable.

Solution

var myVar;

if (typeof myVar != 'undefined') {
    // logic goes here
}

Similar Approach

myVar === undefined
myVar === void 0
typeof myVar == 'undefined'
typeof myVar === 'undefined'
Tags: JavaScripttypeofundefined

Leave a Comment