This snippet results in a JavaScript runtime error: (foo
is not defined)
if (foo) {
// ...
}
I have to define foo
first, like so:
var foo = foo || null // or undefined, 0, etc.
... And only then can I do:
if (foo) {
// ...
}
Why is that?
Update:
This was somewhat of a brainfart on my side of things: 'fcourse you can't access a variable which is not allocated.
Fun stuff that you can do a typeof() on an undefined variable thou. I'm gonna accept miccet's answer since I think it's the most elegant solution.