Reputation: 12211
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?
Upvotes: 9
Views: 23419
Reputation: 120
if (variableName){
////implementation
}
this way is more use full than second option
Upvotes: 3
Reputation: 162791
Your second way is the most reliable but you don't need the parenthesis for the typeof
operator. See this question.
Upvotes: 6