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 6535097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:19:55+00:00 2026-05-25T10:19:55+00:00

var obj; if (!obj) obj = {}; obj.foo = {}; // … I see

  • 0
var obj;
if (!obj) obj = {};

obj.foo = {};

// ...

I see this in the beginning of source code all the time. I don’t understand why someone would want to resort to such an unnecessary approach to creating an object. As per the if statement, of course it’s going to evaluate to true because obj is undefined. So why do people want to create their objects this way? Is there so sort of benefit I don’t know about?

  • 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-25T10:19:56+00:00Added an answer on May 25, 2026 at 10:19 am

    This is a common practice when using an object to help manage the global namespace. For example, in my code, I have a set of global functions spread across a whole bunch of source files. They are grouped into files according to functionality and, in any given project, I may only include use some of the files. So, each file needs to stand alone.

    Yet, all the functions are declared like this:

    JLF.parseColorValue = function (colorStr) {...};
    

    where JLF is my global library object that keeps everything else out of the global namespace.

    That above line of code will only work if JLF already exists as an object so the parseColorValue function can be added to it as a property.

    So, in order to make each of these files independent of all the others and not have to worry about pre-declaring JLF in some master file of each project, I include this line at the beginning of each file:

    if (!JLF) {var JLF = {};}
    

    So, whichever source file gets included into the project first will create the JLF object and all other source files will use the previous declaration and not create another one or assign over the top of the previous one.


    FYI, I’ve also see it done this way:

    if (typeof JLF === "undefined") {var JLF = {};}
    

    When there are sub-objects that you want to make sure are declared, you can do that like this:

    JLF.config = JLF.config || {};
    

    JLF must already exist when you do this, but this will make sure that JLF.config exists.

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

Sidebar

Related Questions

Sometimes I'll see code like this: var Obj = Obj || {}; What does
Is it like... var obj = new Object(); obj.function1 = function(){ //code } or
Objects in javascript throw me for a loop! In this set up... var obj
Given something like var obj = { foo: function(){ try{ doSomething(); }catch(ex){ //@TODO -
var objs = new Array(); function Foo(a) { this.a = a $(#test).append($(<button></button>).html(click).click(this.bar)); } Foo.prototype.bar
var obj = {} obj.__setitem__ = function(key, value){ this[key] = value * value }
var obj = { 'foo' : 'bar', 'something very, very, very, very long' :
Using this type: class Foo { public static implicit operator int(Foo obj) { return
var obj = { destroy: function(){this = null;} }; obj.destroy(); This works in Chrome,
I have an object, defined thus: var obj = { 'title': 'foo', 'id': '123',

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.