This is similar to a number of other questions about SO, but not quite the same as what I can find.
What is the best approach for checking undefined value in Javascript and why?
First example:
var a;
if (typeof(a) === 'undefined'){...}
Second example:
var a;
if (a === undefined){...}
So, the first example compares the type name with the string, and the second compares the variable with the undefined object using the equality operator, which checks that the types are the same as the values.
What's better? Or are they both good to each other?
Please note that I am not asking about any difference between undefined and null, or truth or falsehood, only which of these two methods is correct and / or better.
source
share