Interview practice question
A loop with setTimeout prints the same value every time
Frontend Engineering · JavaScript Core · Beginner
A loop using var and setTimeout prints the final value several times instead of each value. Why, and how do you fix it?
What a strong answer covers
Your AI report card counts how many of these ideas you cover.
- var is function-scoped: one shared variable.
- Callbacks run after loop ends.
- Use let for per-iteration binding.
- Or closure/IIFE.
- Understand async timing.
Practise this topic
Start without signing up. Sign in free to get an AI report card on your answer.