Consider this code:
var functions = []
for(var i = 0; i < 3; i++) {
functions.push(function() {
console.log(i)
})
}
for(var i = 0; i < 3; i++) {
functions[i]()
}
//outputs:
// 1
// 2
// 3
functions.map(function(fn, i) {
fn()
})
//outputs:
// 3
// 3
// 3
Why is the output different between the for loop and array.map? Each function in functions array should output the value 3, this is the typical behavior i would expect from my knowledge in Javascript...