0

Possible Duplicate:
What is “var _gaq = _gaq || []; ” for ?

var _gaq = _gaq || [];

I'm not sure what this line is doing? Can someone explain this to me?

Community
  • 1
  • 1
john
  • 30,030
  • 12
  • 43
  • 60

3 Answers3

3

This is similar to doing

var _gaq = _gaq ? : _gaq : [];

It means that if _gaq is set, it'll set it to _gaq, otherwise it will default to a new empty array.

  • var means it's local scope
  • _gaq is the name of the variable
  • || means or

It's saying that if _gaq doesn't already exist, set it to a new array which is what [] means.

mauris
  • 41,504
  • 15
  • 94
  • 130
AlanFoster
  • 7,818
  • 5
  • 34
  • 52
1

It declares a variable named _gaq. If that variable was already defined, and is a truthy value, then the line is equivalent to writing

var _gaq = _gaq;

If _gaq is a falsy value, then the newly declare variable is an empty array.

Some reference on truthiness and falsiness in JavaScript:

Matt Ball
  • 344,413
  • 96
  • 627
  • 693
0

It checks if _gaq is defined if not assigns an array object to _gaq.

its equivalent to

if(!_gaq){
 var _gaq = [];
}
Chandu
  • 78,650
  • 19
  • 129
  • 131