---
title: "Discussion on \"Your Callback Printed 3, 3, 3: JavaScript Closures Capture the Variable, Not the Value\" | Hashnode"
description: "Discussion on \"Your Callback Printed 3, 3, 3: JavaScript Closures Capture the Variable, Not the Value\". This loop is supposed to log 0, 1, 2:\nconst out = [];\nfor (var i = 0; i < 3; i++) setTimeout(() => out.push(i), 0);\n// later: out is [3, 3, 3]\n\nSwap var for let and you get [0, 1, 2]. Most explanation"
url: "https://hashnode.com/posts/your-callback-printed-3-3-3-javascript-closures-capture-the-variable-not-the-value/6af69f883b0e67b41bf677e7"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Your Callback Printed 3, 3, 3: JavaScript Closures Capture the Variable, Not the Value](https://ankitkhoiwal.hashnode.dev/your-callback-printed-3-3-3-javascript-closures-capture-the-variable-not-the-value?utm_source=hashnode&utm_medium=feed)

This loop is supposed to log 0, 1, 2: const out = \[\]; for (var i = 0; i < 3; i++) setTimeout(() => out.push(i), 0); // later: out is \[3, 3, 3\] Swap var for let and you get \[0, 1, 2\]. Most explanation

4d ago · 5 min read · ankitkhoiwal.hashnode.dev  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#web-development](https://hashnode.com/tag/web-development)
-   [#programming-tips](https://hashnode.com/tag/programming-tips)
-   [#beginners](https://hashnode.com/tag/beginners)

## Written by

[Blogs World](https://hashnode.com/@blogs-world)

@blogs-world

  
[View profile](https://hashnode.com/@blogs-world)

Beyond the code, a story

## Responses

Markdown supported · ⌘↵ to post

No responses yet.
