79
rated 0 times
[
86]
[
7]
/ answers: 1 / hits: 26082
/ 15 Years ago, tue, february 24, 2009, 12:00:00
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.
More From » javascript