I'm reading You Don't Know JS, and the following example confuses me a little bit:
function foo () {
function bar (a) {
i = 3; // changing the `i` in the enclosing scope's for-loop
console.log(a + i);
}
for (var i = 0; i < 10; i++) {
bar(i * 2); // oops, infinite loop ahead!
}
}
foo();
Wouldn't the engine look up (and stick to the assignment) of i inside the for loop, since it's declared and inside the scope of foo?
Why would it go to the i inside of bar?
Furthermore, even if i's value was reassigned to 3, wouldn't it still go up one every time, thus avoiding the infinite loop?
iinside ofbar()is the same one as theiinside offoo()