Scope - let
by Ryan Morris
JavaScript
// Iterating with a function expression
// can cause issues due to scope misuse
/*
for (var i=0; i<3; i++) {
setTimeout(function(){
console.log(i);
}, 1000*i);
} /**/// what will this output?
// using "let"
/*
for (var i=0; i<3; i++) {
let j = i; // scoped to block
setTimeout(function(){
console.log(=j);
}, 1000*i);
}/**/
// better yet... a for-let loop
// it scopes itself per iteration
/*
for (let i=0; i<3; i++) {
setTimeout(function(){
console.log(i);
}, 1000*i);
} /**/