See the code, which explains more than 1000 words. I don't get it, why all 5 created functions always return 5 (last iteration value). I am really interested, what's wrong or what I have missed. I remember dare, Douglas Crockford has talked about this thing, but I didn't find that resource either.
function createFunctions(n) {
var arrayFunctions = [];
for (var i=0; i<n; i++) {
arrayFunctions.push(function() {
console.log(i);
});
}
return arrayFunctions;
}
var arrayFunctions = createFunctions(5);
arrayFunctions[2](); // returns 5 instead of 2
arrayFunctions[3](); // returns 5 instead of 3
arrayFunctions[4](); // returns 5 instead of 4