Below are the 2 scenarios
for (var i=0; i<3; ++i)
{
let num = i;
setTimeout(function() { alert(num); }, 10);
}
Output series: Alerts : 0,1,2
for (var i=0; i<3; ++i)
{
// variables introduced in this statement
// are scoped to the block containing it.
let num = i;
setTimeout(function() { alert(num); }, 10);
}
Output series: Alerts : 0,2,1
Two identical code but with different result.
Any Idea ???