Why does the JavaScript expression "ga = ga || []" work?

Below javascript statements will throw an error if ga is not declared.

if (ga) { alert(ga); } 

Mistake:

 ga is not defined 

It seems that an undefined variable cannot be recognized in a bool expression. So why does the bottom statement work?

 var ga = ga || []; 

For me, ga is treated as the value of bool before "||". If it is false, the expression after the word "||" assigned to the final ga.

+7
javascript syntax
source share
6 answers

Change You must use var ga; first or var ga = ga || []; var ga = ga || []; because you first declare ga and assign values ​​to it.

You can try this

 var x = 1, y = x + 1; alert([x,y]) // 1,2 

You may notice that when y is declared, x is already declared and already assigned to 1 to it.

So, in your case, when ga || [] ga || [] assigns ga is already declared and its valid variable.

+3
source share

null or false values ​​are defined in javascript (implicitly evaluates to false). || the operator returns the first value, which is not evaluated as false.

 var x = 0 || "" || null || "hello" || false; // x equals "hello" 

On the other hand, the && operator will return the first false or the last value.

 var y = "a string" && {"attr":"an object"} && false && ["array"]; // y equals false var z = "a string" && {"attr":"an object"} && ["array"]; // z equals ["array"] 
+4
source share

This works in the case of 'var' because a stop variable is created in the scope resolution. Without the "var", you simply run away looking through the chains of regions and you are informed . If you really want to use a global variable:

 // This is fine because assignment always sets a property value // in this case (no 'var ga') it the same as window.ga = window.ga || [] ga = window.ga || [] 

Or:

 // Once again, only the lookup is affected with "running off" // the lookup chain. It not that the variable has no value -- // the problem is there IS NO VARIABLE. if (!window.ga) { ga = [] } 

Or even this:

 // First line is the same as window.ga = window.ga, // but now the property ga is guaranteed to exist on the window object -- // the VALUE of the property may (still) be undefined ga = window.ga ga = ga || [] 

Note that in both cases, I explicitly called ga as a property of the window (global) object.

Here you can read the details: Identifier Resolution, Execution Contexts, and Area Chains .

The placement of var within a region does not matter. Everything is the same:

 var ga ga = ga || [] var ga = ga || [] ga = ga || [] var ga 
+1
source share

Perhaps a less strange option:

 if (typeof ga !== 'undefined') { alert(ga); } 
+1
source share

In the second statement, ga is checked if it is defined and if true, assigned to itself. Otherwise, it was assigned an empty value.

0
source share

You expect b to be an array, maybe you want to use the array method on b or read the index value from it.

 var b= s.match(/\d+/) || []; return b[0]; 

it returns consistent text or undefined -

without an empty array assignment, the second line will throw an error if there was no match.

0
source share

All Articles