Why JavaScript Statement "ga = ga || []" Works?
- by Morgan Cheng
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.