1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

||{ } What does it mean in Javascript?

Discussion in 'General Programming Chat' started by redfishyellowfish, Feb 22, 2013.

  1. redfishyellowfish

    redfishyellowfish Junior Member

    Joined:
    Nov 1, 2012
    Messages:
    109
    Likes Received:
    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.
     
  2. redfishyellowfish

    redfishyellowfish Junior Member

    Joined:
    Nov 1, 2012
    Messages:
    109
    Likes Received:
    10
    Nevermind.
    Simple google search.
     
  3. ikstob

    ikstob Junior Member

    Joined:
    Nov 12, 2012
    Messages:
    147
    Likes Received:
    129
    Location:
    ikstob.com
    Home 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.
     
    • Thanks Thanks x 1
  4. prosper109

    prosper109 Junior Member

    Joined:
    Mar 3, 2013
    Messages:
    165
    Likes Received:
    12
    I think the double || means that its OR'ing the variable which in computer terms will behave differently for different values.