I have been using typeof
operator to check if a variable is not defined like this:
if( typeof numLines === "undefined" ){
// do something
}
But the same can be achieved using:
if( numLines === undefined ){
// do something
}
As far as I remember, I had read somewhere that the typeof approach is better but now I think why should I use a slightly longer statement if there is no benefit at all in that. So my questions are:
- Is there really any benefit in sticking to
typeof
approach for such checks? - Can you provide some examples where one approach is better than the other?
Thanks.