9

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?

3

2 Answers 2

6

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

3
if (variableName){
////implementation
}

this way is more use full than second option

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.