Created
May 5, 2017 12:06
-
-
Save MaraAlexa/df444392a1baa4e9caaa087d85bdf092 to your computer and use it in GitHub Desktop.
ES6: for loop issue solved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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