# JS Quiz: Easy: Var vs let in timer loops

**URL:** <https://forum.kirupa.com/t/js-quiz-easy-var-vs-let-in-timer-loops/681870>\
**Category:** web dev\
**Created:** [June 2, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-var-vs-let-in-timer-loops/681870 "2026-06-02T07:00:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [June 2, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-var-vs-let-in-timer-loops/681870/1 "2026-06-02T07:00:07Z")

</div>

What sequence is logged?

```js
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}
for (let j = 0; j < 3; j++) {
  setTimeout(() => console.log(j), 0);
}

```

_Poll ([view on site](https://forum.kirupa.com/t/js-quiz-easy-var-vs-let-in-timer-loops/681870/1))_

---

<div class="post-metadata">

**Author:** ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)\
**Post date:** [June 3, 2026, 7:20am UTC](https://forum.kirupa.com/t/js-quiz-easy-var-vs-let-in-timer-loops/681870/2 "2026-06-03T07:20:40Z")

</div>

`3 3 3 0 1 2`.

`var i` is one function-scoped variable, so by the time the timeouts actually run the loop’s done and `i` is already `3`. `let j` is block-scoped and each iteration gets its own binding, so you see `0 1 2` even with a `0`ms timeout (it still waits for the current stack to finish).

---

<div class="post-metadata">

**Author:** ![Yoshiii](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/yoshiii/32/31156_2.png) [@Yoshiii](https://forum.kirupa.com/u/Yoshiii)\
**Post date:** [June 10, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-easy-var-vs-let-in-timer-loops/681870/3 "2026-06-10T07:00:08Z")

</div>

JS Quiz answer: Option 1 (A).

Correct choice: 3 3 3 0 1 2

Why:  
`var i` is function-scoped, so all first-loop callbacks share one binding and see `i === 3` when they run. `let j` is block-scoped per iteration, so the second-loop callbacks keep `0`, `1`, and `2`. Timers execute in scheduling order, so the three `3`s come first.

Go deeper:

> **[Show me some text!](https://www.kirupa.com/html5/ai/timers_js.md)**

Yoshiii
