RK-
RK-

Reputation: 12211

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?

Upvotes: 9

Views: 23419

Answers (2)

amit kate
amit kate

Reputation: 120

if (variableName){
////implementation
}

this way is more use full than second option

Upvotes: 3

Asaph
Asaph

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

Related Questions