Q:

Shouldn't an uninitialised var always be of type undefined

I came upon a case where var behaves counter intuitively.
Here is an example:
for (var i = 0; i < 10; i++) {
    var toRun;

    if(i%2 !== 0) {
        toRun = {number:i};
    }

    console.log("toRun is", toRun);

    if ('undefined' != typeof toRun && 'undefined' != typeof toRun.number)
    {
        //Do something
    }
}

The result when running with node js is:
toRun is undefined
toRun is { number: 1 }
toRun is { number: 1 }
toRun is { number: 3 }
toRun is { number: 3 }
toRun is { number: 5 }
toRun is { number: 5 }
toRun is { number: 7 }
toRun is { number: 7 }
toRun is { number: 9 }

I was expecting:
toRun is undefined
toRun is { number: 1 }
toRun is undefined
toRun is { number: 3 }
toRun is undefined
toRun is { number: 5 }
toRun is undefined
toRun is { number: 7 }
toRun is undefined
toRun is { number: 9 }

Is this normal behaviour in javascript?
Shouldn't toRun var toRun set toRun to undefined each time the code in the loop is executed?
node -v
v0.10.33

A:

Only functions create new scopes in JavaScript.  Declarations of variables are hoisted to the top of the innermost function.
function one() {
  var a;
  console.log(a);
  a = 1;
}

function two() {
  console.log(a);
  var a = 1;
}

function three() {
  console.log(a);
  for(var i = 0;i < 10;i++) {
    var a;
  }
}

These three functions have the same behavior - they print undefined.  Note that the declaration is hoisted, but not the initialization.

