Looking at this code, if I console log i
after the loop terminates, i = 10
but I don't understand how.
i
is set to 0
and then increments by one as long as i < 10
, so it stops incrementing at 9
. So in the log within the loop, i counts from 0 - 9, but when I log i
AFTER the loop terminates. It has become 10.
I don't understand how it goes from 9 - 10.
"use strict";
var foo = [];
for (var i = 0; i < 10; i++) {
console.log(i);
foo[i] = function() { return i};
}
console.log(i);
Could anyone explain this to me? Thank you :)