var myAlerts = [];
for (var i = 0; i < 5; i++) {
myAlerts.push(
function inner() {
alert(i);
}
);
}
myAlerts[0](); // 5
myAlerts[1](); // 5
myAlerts[2](); // 5
myAlerts[3](); // 5
myAlerts[4](); // 5
why the all the values at positions 0-4 in array are "5". I found some article regarding this as Javascript supports Lexical scoping rather than Dynamic. Yes, I got that. But is there any way to understand this core concept for the above snippet Thank you