You can never read variables that have not been declared, and what you are trying to use in the expression _gaq || [] _gaq || [] in the latter case.
In this case
_gaq = _gaq || [];
_qaq not been declared before, and when the right side is evaluated ( _gaq || [] ), it throws an error.
Here is a step-by-step explanation of what happens in this case:
The assignment operator is described in section 11.13.1 of the specification:
The product AssignmentExpression : LeftHandSideExpression = AssignmentExpression is evaluated as follows:
1. Let lref be the result of evaluating LeftHandSideExpression .
2. Let rref be the result of an AssignmentExpression evaluation.
...
LeftHandSideExpression - _gaq , AssignmentExpression - _gqa || [] _gqa || [] .
So, _qaq is evaluated _qaq , which leads to an unsolvable reference , since the _gaq variable _gaq not declared. This rating does not cause an error.
Then evaluated _gqa || [] _gqa || [] . This is LogicalORExpression and is described in Section 11.11 as LogicalORExpression || LogicalANDExpression LogicalORExpression || LogicalANDExpression . In this case, the LogicalORExpression , on the left, _gaq and LogicalANDExpression , the right, is [] .
The expression is evaluated as follows:
1. Let lref be the result of evaluating LogicalORExpression .
2. Let lval be GetValue(lref) .
...
We already know that lref will be an unsolvable link because _gaq not declared. So, let's see what GetValue does (defined in section 8.7.1 , V is the value passed to GetValue )
1. If Type(V) not Reference , return V
2. Let base be the result of calling GetBase(V) .
3. If IsUnresolvableReference(V) , throw a ReferenceError exception.
...
As you can see, in the third step of this procedure, a ReferenceError is raised, which, in turn, is executed evaluating the right-hand side of the job, and an error occurs here.
So why this does not happen with var _gaq = _gaq || []; var _gaq = _gaq || []; ?
This line:
var _gaq = _gaq || [];
actually
var _gaq; _gaq = _gaq || [];
due to something called hoisting [MDN] . This means that when evaluating _gaq it will not lead to an unsolvable link, but a link with the value undefined .
(If the _gaq variable _gaq already declared (and potentially has a value), then var _gaq will have no effect.)
If you want to create _gaq globally from within a function, do it explicitly by referring to window :
window._gaq = window._gaq || [];