Skip to content

Instantly share code, notes, and snippets.

@ecasilla
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save ecasilla/17946f3a07bc99085af4 to your computer and use it in GitHub Desktop.

Select an option

Save ecasilla/17946f3a07bc99085af4 to your computer and use it in GitHub Desktop.
Using this as a way to show students about closures and scope
for (var i = 1; i <=5; i++){
setTimeout(function(){
console.log("vaule of i is: " + i);
},i*1000);
}
for (var i = 1; i <=5; i++){
(function(i){
setTimeout(function(){
console.log("vaule of i is: " + i);
},i*1000);
})(i)
}
++++++++++++++++++++++++++++++++++++++
ES6 Way
for (let i = 1; i <=5; i++){
setTimeout(function(){
console.log("vaule of i is: " + i);
},i*1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment