DEV Community

Discussion on: Pass parameter to setTimeout inside a loop - JavaScript closure inside a loop

Collapse
 
laphilosophia profile image
Erdem Arslan

or you do that:

for (let i = 0; i < arr.length; i++) {
  (e => {
    setTimeout(() => {
      return console.log(e)
    }, 100)
  })(i)
}

anyway, all of them work perfectly