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:
let
var
for(let i=0; i<5; i++) { setTimeout((function() { console.log(i) }), 1000) }
since let variables are bound to the block scope :)
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Just a small addition:
instead of
you can also just use
let
, instead ofvar
here:since
let
variables are bound to the block scope :)