Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript: is this a conditional assignment?

Tags:

javascript

From the google analytics tracking code:

var _gaq = _gaq || [];

how does this work?

Is it a conditional variable value assignment? Is it the same as saying:

if( !(_gaq) ) {_gaq = []; }

?

like image 511
mikkelbreum Avatar asked May 29 '11 13:05

mikkelbreum


1 Answers

The or operator (||) will return the left hand side if it is a true value, otherwise it will return the right hand side.

It is very similar to your second example, but since it makes use of the var keyword, it also establishes a local scope for the variable.

like image 126
Quentin Avatar answered Oct 16 '22 13:10

Quentin