Out of just intellectual curiosity, why does javascript accept
var z = z || [];
to initialize z (as z may defined initially)
but without var, it throws an error (in global space)
z = z || [];
(if z is previously undefined)
In the global space you are not required to use VAR though I get it might be bad practice.
Before you say this is a duplicate of questions like
What is the purpose of the var keyword and when to use it (or omit it)?
Note the declaration that "If you're in the global scope then there's no difference."
Obviously that's not 100% true, given my working example.
Is this a quirk or is there legitimate logic?
adding a summary of the answer as I've learned it:
Thanks to Tim (see below) the key to my misunderstanding was not realizing this (fundamental of javascript)
var z; does absolutely nothing if z already exists
That's how this expression seems to have it both ways, if you incorrect assume that "var z" always initializes.
Starting from the left, "var z" simply makes sure z is defined but does not actually affect any existing value if it already exists. Then on the right, if z already exists, it is used, if not, the variable was just declared (but empty) so it won't be used but will not throw an error.
This is an excellent article on this kind of scoping and hoisting issue in Javascript: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting
Many thanks to minitech and everyone else who contributed too!