How to check if value is “undefined” in Javascript

TypeOf

If we want to know if the value of a variable is “undefined” in javascript, we can use “typeof” javascript operator. It returns a string and it doesn’t generate an error if the variable doesn’t exist at all.

Javascript:

if( typeof value === "undefined" ){
    // ...
}

And we could define an specific method to check it:
Javascript:

function isUndefined(value){
    return ( typeof value === "undefined" );
}

var varName = "hello";
if( isUndefined(varName) ){
    //do this
}else{
    //do this
}

Reference link: http://stackoverflow.com/questions/10653367/how-to-check-undefined-value-in-jquery

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s