Skip to content

Instantly share code, notes, and snippets.

@MaraAlexa
Created May 5, 2017 12:06
Show Gist options
  • Save MaraAlexa/df444392a1baa4e9caaa087d85bdf092 to your computer and use it in GitHub Desktop.
Save MaraAlexa/df444392a1baa4e9caaa087d85bdf092 to your computer and use it in GitHub Desktop.
ES6: for loop issue solved
// BAD
for(var i = 0; i < 10; i++) {
console.log(i); // 0 1 2 3 4 5 6 7 8 9
setTimeout(function() {
console.log('The number is ' + i);
},1000);
} // -> 'The number is 10' printed 10 times
// GOOD
for(let i = 0; i < 10; i++) {
console.log(i); // 0 1 2 3 4 5 6 7 8 9
setTimeout(function() {
console.log('The number is ' + i);
},1000);
}// you get each number printed (from 0 to 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment