2
  ////Creating Object
    var Obj;

  // init Object
  Obj= {};
  1. What's the difference between these two?
  2. Is it possible to make it into a single line?
  3. Any Advantages of using like this?

10 Answers 10

7

the first declares a variable in the current scope, it does not assign any value to it,thus it is not creating any object, while the second one creates an empty object literal to which you can refer through the global Obj variable.
There are 2 main differences : scoping variables and initializing vs declaring.
ex :

var Obj;
alert(typeof Obj);//='undefined'

//scoping : 
function foo(){
    obj1 = {};
    var obj2 = {};
}
alert(typeof obj1);//='object'
alert(typeof obj2);//='undefined'
// obj2 is visible only in the foo function scope,while
// obj1 is being attached to the global scope, the window object
// you can access obj1 as window.obj1 too
Sign up to request clarification or add additional context in comments.

Comments

6

var Obj; is not creating an object, it is declaring the variable Obj.

Obj = {}; is initializing the variable Obj with an empty object.

Of course you can combine it and do it in one go:

var Obj = {};

You basically just have to separate declaration and initialization, if you want to initialize the variable with different values, based on some condition or if the variable should be declared in another scope. For example:

var Obj;

if(something) {
    Obj = 'me';
else {
    Obj = 'you';
}

1 Comment

@theJava: Still the same, only the name changed ;)
2

var Obj; does not create an object, it declares a variable.

Obj = {}; assigns an empty object to the variable named Obj. If Obj was not previously declared, this implicitly declares the variable in the global scope, since the var keyword is missing (don't do this).

Comments

2
var Obj;

only declares the Obj variable, whereas

var Obj = {};

declares and initializes the Obj variable as an empty object.

Make sure you understand the use of the var keyword here as well. It can be used in declaring and/or initializing variables. The difference between

Obj = {};

and

var Obj = {};

is that the var-less code initializes Obj in the global namespace (often considered a bad practice).


tl;dr: In most cases, it is best to initialize and declare at the same time in the local scope, using the var keyword:

var Obj = {};

Comments

1

Maybe I'm not totally understanding your question, but...

var Obj= {};

Is totally fine.

Comments

1

The first allocates the slot for the variable Obj but leaves it undefined. The second binds a newly created object to the name Obj.

You can do it in one line: var Obj = {};. Doing it in two steps has no real advantages, only risks.

Comments

1
var Obj;

this is not creating an object. it defines a variable;

object is first created and than initialized so this are two key processes that are both necessary to have an object.

Comments

1

This line does not actually create an object. Rather it creates a variable (or a place that could hold an object if you put one in there).

var Obj;

This line, does create an object, and places it into the variable.

Obj = {};

This line "initializes" the object by creating a property and setting it to something.

Obj.Something = null;

Comments

1

Note that by saying

var Obj;

you are not creating the object, merely defining a (in scope of a function local) variable initially set to null

Other than that there's no difference whether you merge it into one statement or not.

1 Comment

Actually, the value is not null but undefined.
0

If that's all the code you've got, there is absolutely NO difference between this:

var Obj;
Obj = {};

and this:

var Obj = {};

IF that's all the code you've got, with nothing in between modifying the Obj variable. In fact, I'd argue that the second way of doing it would be faster than the two lines above.

3 Comments

I see it there. Yeah, you'd be totally fine combining them into one line, and I'd recommend doing so.
well, there is A BIG difference : the type of the variables. undefined vs object, not to mention the scope
@gion_13 I'd argue that you're incorrect, and definitely didn't deserve the downvote. The two code snippets that I have above will behave exactly the same when used as-is with nothing in between, as I've specified in my answer. I'm talking purely from an implementation perspective, not from what actually happens behind-the-scenes, as yes Obj is set to undefined for ONE LINE where nothing happens after it except it being set to {}.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.