The highest answer is correct, use typeof.
However, what I wanted to point out was that in JavaScript undefined
is mutable (for some ungodly reason). So simply doing a check for varName !== undefined
has the potential to not always return as you expect it to, because other libs could have changed undefined. A few answers (@skalee's, for one), seem to prefer not using typeof
, and that could get one into trouble.
The "old" way to handle this was declaring undefined as a var to offset any potential muting/over-riding of undefined
. However, the best way is still to use typeof
because it will ignore any overriding of undefined
from other code. Especially if you are writing code for use in the wild where who knows what else could be running on the page...