How would you fix the classic setTimeout-in-loop closure problem to log 0, 1, 2?

JavaScript Developer Medium

JavaScript Developer — Medium

How would you fix the classic setTimeout-in-loop closure problem to log 0, 1, 2?

Key points

  • Using let creates a new binding for i in each iteration
  • var creates a single binding shared across all iterations
  • This prevents the closure from capturing the final value of i

Ready to go further?

Related questions