3 ms·
I generally prefer: var banana; if (!banana) babana = "banana"; or var banana; banana = banana || "banana"; I have a strong dislike for impl
by masklinn 14y ago
I generally prefer:
var banana;
if (!banana) babana = "banana";
or
var banana;
banana = banana || "banana";
I have a strong dislike for implicit globals — even at the toplevel — and the global object isn't necessarily `window` (it's not in the Spidermonkey or Node shells for instance, I'm not sure it is in Web Workers — there's a WorkerGlobalScope object but i don't know if it's bound to a global called `window`).
If `undefined` has to be checked for explicitly (because `null` or `0` or `""` are valid values), check `banana === undefined` (or `banana === void 0` if your coworkers are assholes)
- thezilch 14y agoI don't disagree; it was just the briefest example, which assumed that window was the scope -- couldn't really think of a reason I'd warrant an inner scope to have uninitialized vars. So, to the OP, you can always check that your var is a member of some object, including window. The typeof example is bad form.