0

I am still trying to fully grasp Javascript. I use it enough in web apps to understand it but not at a deep level. The first line of Google Analytics ( and any analytics script it seems ) creates an array IF one does not exist. I don't understand how that works, how would it know if it exists. My research keeps turning up the global queue but I cannot fund enough info on it. Here is the line at the beginning of both FB and GA javascript include snippet:

var _fbq = window._fbq || (window._fbq = []);

var _gaq = _gaq || [];

I take that eo be var X = the existing array for this name if it exists OR create a new empty array. How does is know if that array exists? To me, again, I would think it will always equal the empty array?

Webtect
  • 819
  • 2
  • 10
  • 31

2 Answers2

0

If you write it this way: var _gaq = _gaq || [];

... the code assigns _gaq it's own value if it exists, or if it doesn't, it creates a default value for it, in this case an empty array ([]).

Essentially the code is short for: if (_gaq === undefined) {var _gaq = [];}

This is called "short circuit evaluation" and has been previously discussed, amont other places, here: JavaScript OR (||) variable assignment explanation

Community
  • 1
  • 1
DocWeird
  • 288
  • 1
  • 7
  • Hi @Olavi thanks for responding. I read through the post and understand the why it evaluates, my questions is how does it even know if, say, gaq exists? Its the first line in the script. Its not like above it there is a _gaq['a' => 'b']; that it looks at first but clearly somewhere _gaq exists somewhere, and my research keeps pointing to this Global Queue which I am trying to wrap my head around. – Webtect Mar 21 '17 at 14:40
0
if (_gaq[1]) { #... }

results in a script error if _gaq is not an array.

This lines makes _gaq an array if it returns a false. But does not remove any data from _gaq if it had data IE returned true.

var _gaq = _gaq || [];

If _gaq[1] = "I have data" then _gaq would of returned a true and _gaq[1] would still contain data.

The fact that if _gaq was an empty array and was tested and returned false and turned into a empty array needlessly is mute. It does not effect program operation. The purpose is that from this point on _gaq is an array and can be used as an array.

This works when _gaq may have been populated with data from another script, without an error.

var _gaq = _gaq || [];
if (_gaq[1]) { #... }
Wayne
  • 4,760
  • 1
  • 24
  • 24
  • Hi @Wayne, So what you are saying is that this Global Queue is NA. That this will always result in an empty array unless there is something else running before this that populated that array? – Webtect Mar 21 '17 at 14:56
  • Exactly, and attempting to access an element in an non-existent array stops to script from continuing it makes it safe for the code that follows to use it as an array. Even though the data may not exist in it yet from a AJAX request or some other script responsible for filling it. – Wayne Mar 21 '17 at 15:01
  • Sometimes on cross server scripts one of the servers may be down. But you still want the local script to do something. – Wayne Mar 21 '17 at 15:04
  • For Google Analytics I believe the local script populates _gaq and the remote script reads it. The remote script becomes aware that the user has forgotten to add the required script to his page and then Google Analytics can tell him to add the script to his page. – Wayne Mar 21 '17 at 15:10