Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1092667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:43:06+00:00 2026-05-16T23:43:06+00:00

Sometimes I’ll see code like this: var Obj = Obj || {}; What does

  • 0

Sometimes I’ll see code like this:

var Obj = Obj || {};

What does this do? I have had success writing

array = array || [];

To instantiate an array if it hasn’t already been instantiated, however I would like to know a bit more about the mechanics of this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:43:07+00:00Added an answer on May 16, 2026 at 11:43 pm

    The technique tries to make use of something called short circuit evaluation… but it’s tricky in Javascript, and turns out to be quite dangerous if you try to make use of it for Object instantiation.

    The theory behind short circuit evaluation is that an OR statement is only evaluated up to the first true value. So the second half of an OR statement is not evaluated if the first half is true. This applies to Javascript……

    But, the peculiarities of Javascript, in particular how undeclared variables are handled, make this a technique that has to be used with great care to instantiate objects.

    The following code creates an empty object except if Obj was previously declared in the same scope:

    var Obj = Obj || {}; // Obj will now be {}, unless Obj was previously defined
                         //  in this scope function.... that's not very useful...
    

    This is because after var Obj, Obj will be undefined unless it was declared in the same scope (including being declared as a parameter to the function, if any)…. so {} will be evaluated. (Link to an explanation of var provided in the comments by T.J. Crowder).

    The following code creates an empty object only if Obj has been previously declared and is now falsy:

    Obj = Obj || {};     // Better make sure Obj has been previously declared.
    

    If the above line is used when Obj has not been previously declared, there will be a runtime error, and the script will stop!

    For example this Javascript will not evaluate at all:

    (function() {
        Obj = Obj || "no Obj"; // error since Obj is undeclared JS cannot read from 
        alert(Obj);​            //   an undeclared variable. (declared variables CAN
    })();                      //   be undefined.... for example "var Obj;" creates 
                               //   a declared but undefined variable. JS CAN try
                               //   and read a declared but undefined variable)
    

    jsFiddle example

    But this Javascript will always set Obj to “no Obj”!

    var Obj ="I'm here!";
    (function() {
        var Obj = Obj || "no Obj"; // Obj becomes undefined after "var Obj"...
        alert(Obj);  // Output: "no Obj"
    })();​
    

    jsFiddle example

    So using this type of short circuit evaluation in Javascript is dangerous, since you can usually only use it in the form

    Obj = Obj || {};
    

    Which will fail precisely when you would most want it to work… in the case where Obj is undeclared.


    Note: I mention this in the comments of the penultimate example, but it’s important to understand the 2 reasons that a variable can be undefined in Javascript.

    1. A variable can be undefined because it was never declared.
    2. A variable can be undefined because it was declared but has not had a value assigned to it.

    A variable can be declared using the var keyword. Assigning a value to an undeclared variable creates the variable.

    Trying to use an undefined variable that is also undeclared causes a runtime error. Using an undefined variable that has been declared is perfectly legal. This difference is what makes using Obj = Obj || {}; so tricky, since there is no meaningful form of the previous statement if Obj is either undeclared OR it is a previously existing variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes I see code like this: LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL&
Sometimes you see code like this. <script type=text/javascript src=https://apis.google.com/js/plusone.js> {lang:'en', parsetags:'explicit'} </script> I'd like
Sometimes there's a fine line between being safe and running scared. Does this code
Sometimes I have come across this issue that whenever I change the order of
Sometimes I'd like to print my code and read it during lunch. In Eclipse
Sometimes we put some debug prints in our code this way printf(successfully reached at
Sometimes, for certain things like writing a line to a console, feels like something
Sometimes GCC generates this instruction when compiling with -march=atom . Does each and every
Sometimes in my code I have a function which can take an argument in
Sometimes I have to work on code that moves the computer clock forward. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.