||{ } What does it mean in Javascript?

redfishyellowfish

Junior Member
Joined
Nov 1, 2012
Messages
109
Reaction score
10
I think it means and or
heres the context:
Code:
var Tumblr=window.Tumblr||{};

I'm just looking at this code to make a new code to allow my posts to appear on a separate page.
 
For others landing on this page :) this statement allows you to initialise a new variable as an empty object/array, if the object already exists it will be reused instead of overwritten. It's frequently found in third-party JavaScript libraries - even Google Analytics does it:

Code:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345678-9']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

And the reason for this is that you can pre-load GA events, custom variables etc. before loading GA itself.
 
I think the double || means that its OR'ing the variable which in computer terms will behave differently for different values.
 
Back
Top