re: JavaScript Closure Simply Explained VIEW POST

FULL DISCUSSION
 

Just a small addition:
instead of

for(var i=0; i<5; i++) {
  setTimeout((function(index) {
    console.log(index)
  })(i), 1000)
}

you can also just use let, instead of var here:

for(let i=0; i<5; i++) {
  setTimeout((function() {
    console.log(i)
  }), 1000)
}

since let variables are bound to the block scope :)

 

Thanks Jurian! I debated if I should include this solution in my article because I just wanted to focus on closure but I am glad you brought it up :)

code of conduct - report abuse