If you want to check for the existence of a variable, and a
not declared anywhere in your script, typeof a === 'undefined'
is the way to go, or you can use if (!window.a)
. Otherwise, a ReferenceError is raised.
Your guess seems correct. An empty string and NaN are also evaluated as false
(or, like some of them, falsy
). The fact that 0 evaluates to false
can be tricky, but it is also convenient in expressions such as while (i--)
(if i
is 0, it evaluates to false
and the while loop stops).
source share