This snippet results in a JavaScript runtime error: (foo
is not defined)
if (foo) {
// ...
}
I have to define foo
That would be because you're now defining it with:
var foo = foo || null
Why don't you define it in the first place? That seems pretty straightforward to me (unless I'm missing something).
Using a variable before it's created (or set to something) is bad programming practice, and should be avoided. My advice is to not use that trick to ensure it is set to something but to track down the logic error and fix it.