The JavaScript Debugging Stories From Real PRs

Four solutions to the same closure-and-var trap, plus one bug that took me an afternoon. 5 questions, all from real PRs where the test passed and the user-visible behavior was still wrong.

Question Bundle
JavaScript
interview-prep
js-language
closures
js-this
marcusreddy

By @marcusreddy

December 30, 2025

·

Updated May 18, 2026

446 views

10

4.4 (9)

First time I saw this in a PR I assumed setTimeout was broken. It logs 5,5,5,5,5 instead of 0,1,2,3,4. Walk through why, without writing the fix yet.

What I want to see

The diagnosis: var is function-scoped, the loop runs to completion before any timer fires, and each callback closes over the same i (now 5). The fix is in q-2 through q-4, not here.

4 more questions and all solutions are locked.

Purchase this item to access all questions, code snippets, and solutions.