3

What's the difference between declaration, definition and initialization? Example:

// Is this a declaration?
var foo;

// Did I defined object in here (but it is empty)?
var foo = {};

// Now that object is initialized with some value?
var foo = {first:"number_one"};
GSerg
  • 76,472
  • 17
  • 159
  • 346
Miroslav Trninic
  • 3,327
  • 4
  • 28
  • 51

4 Answers4

5

The first example is a declaration. You have declared a variable with the identifier foo. You haven't given it a value yet, so it will be undefined:

var foo;
console.log(foo); //undefined

The second example is a declaration and an assignment. You have assigned an empty object literal to the variable with the identifier foo. As noted in the comments, this is effectively short for:

var foo;
console.log(foo); //undefined
foo = {};
console.log(foo); //Object

The third example is another declaration and another assignment. You have assigned a different object literal to foo.

Edit (see comments)

The behaviour of your code is slightly different depending on whether you intended each example to run as an independent program, or as written (one program).

If you treat is as it's written:

Because variable declarations in JavaScript are hoisted to the top of the scope in which they appear, redeclaring variables has no effect. So the first line declares a variable foo.

The second line assigns an empty object literal to foo, and the third line assigns a different object literal to foo. Both of these assignments apply to the same foo.

What effectively happens is this:

var foo;
foo = {}; //No `var` keyword
foo = {first:"number_one"}; //No `var` keyword

If you treat each line as a separate program:

The first program declares a variable named foo. It's value is undefined.

The second program declares a variable named foo, and then assigns an empty object literal to it.

The third program declares a variable named foo and then assigns an object literal with one property to it.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • The last two are also declarations. `var a = b;` is shorthand for `var a; a = b;`. – pimvdb Feb 26 '12 at 12:13
  • @pimvdb - Very true, I didn't notice the use of `var` in 2nd and 3rd examples (I assumed the first example was declaring `foo`, and the same `foo` was being reused in the other examples). – James Allardice Feb 26 '12 at 12:14
  • 1
    @pimvdb - Actually, is it not just an assignment? Redeclaring `foo` won't have any effect (since declarations are hoisted), so any "declarations" of `foo` following the initial declaration won't really be declarations, but rather just assignments. Unless of course you take all 3 examples as separate programs. – James Allardice Feb 26 '12 at 12:20
  • 1
    Touche. If the statements are all run in one function, then the last two `var`s have no effect. – pimvdb Feb 26 '12 at 12:21
2

You got it right.

var foo;        // Is this a declaration ?

Yes, you declared that there's a variable named foo, but didn't define foo (so foo is undefined).

var foo = {}    // Did I defined object in here (but it is empty) ?

Yes, now you "defined" foo... it has a value, it is no longer undefined. var foo = 5 also counts as "defining" it.

var foo = {first:"number_one"}  // Now that object is initialized with some value ?

You could say that it's "initialized," but that's really just semantics. "Declared" and "defined" are a bit more meaningful.

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
0

Run the code below:

var foo;
console.dir(foo);
var foo = {};
console.dir(foo);
var foo = {first:"number_one"};
console.dir(foo);
xdazz
  • 158,678
  • 38
  • 247
  • 274
0

When you declare a variable with var foo; you actually ensure that it will belong to the scope where you've defined it. What you call definition and initialization is in fact a value assignment.

Consider following piece of code as an example:

(function () {

  // definition
  var foo;

  function assignFoo(x) {
    // assignment
    foo = x;
  }

  assignFoo(5);

  console.log(foo);

})();

To say, you're not always supposed to assign value within the scope of definition. But it is the most common use case which is usually accomplished with var foo = 5.

Thats it.

mcmlxxxiii
  • 913
  • 1
  • 9
  • 21