Both answers by @thefourtheye and @Remigius Kijok are correct. Whenever variables are declared and initialized within functions (or within the global scope), the declaration gets hoisted to the top of that scope.
You can see this for yourself by wrapping either of the for
loops you have within a function and then executing the function in your browser. Specifically, by setting a break point at that top of the function, you'll be able to see that all the variables are declared and initialized with the value undefined
prior to the function executing.
var loopy = function () {
for (var i = 0; i < 5; i += 1) {
for (var j = 0; j < 3; j += 1) {
console.log('i is ' + i + ' and j is ' + j);
}
}
};
loopy(); // place break point here

As you can see in the photo, I'm using Chrome's dev tool and placing a break point on the execution of loopy()
. After entering the function, but prior to executing any of the for
loops, the variables i and j are both declared but undefined
.