Below javascript statements will cause error, if ga is not declared.
if (ga)
{
alert(ga);
}
The error is:
ga is not defined
It looks undeclared variable cannot be recognized in bool expression. So, why below statement works?
var ga = ga || [];
To me, the ga is treated as bool value before “||”. If it is false, expression after “||” is assigned to final ga.
Edit: You need to use
var ga;first orvar ga = ga || [];, because its declare ga first and assign values into it.You could try this
You may notice, when y is declare, x is already declared and already assigned 1 to it.
So, in your case, when
ga || []assigns, ga is already declared and its valid variable.