How to check if undefined in 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.


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