简体   繁体   中英

Checking for Undefined in Javascript

I was following the below logic to check if a variable is undefined or not:

 if (variable==undefined){
////implementation
}

But found that for some cases it did not function as expected. So, tried this approach,

if(typeof(variable) == "undefined"){
/////implementation
}

So which one is most reliable?

Your second way is the most reliable but you don't need the parenthesis for the typeof operator. See this question .

if (variableName){
////implementation
}

this way is more use full than second option

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM