---
title: "Discussion on \"Simplifying Closures in JavaScript\" | Hashnode"
description: "Discussion on \"Simplifying Closures in JavaScript\". A closure is the combo of a function bundled together with references to its surrounding state (the lexical environment)."
url: "https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6"
---

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

Discussion

# [Simplifying Closures in JavaScript](https://ayeshasahar.hashnode.dev/simplifying-closures-in-javascript?utm_source=hashnode&utm_medium=feed)

A closure is the combo of a function bundled together with references to its surrounding state (the lexical environment).

Aug 1, 2022 · 5 min read · ayeshasahar.hashnode.dev  
Updated Aug 3, 2022  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#tutorial](https://hashnode.com/tag/tutorial)
-   [#web-development](https://hashnode.com/tag/web-development)
-   [#webdev](https://hashnode.com/tag/webdev)

## Written by

[Ayesha Sahar](https://hashnode.com/@IAyeshaSahar)

@IAyeshaSahar · Pakistan

  
[View profile](https://hashnode.com/@IAyeshaSahar)

Developer | Designer | Tech Blogger

Interested in Python🐍 C++🤓 UI/UX 🌸 Web Development✨

## Responses 4

Markdown supported · ⌘↵ to post

[Hrishikesh Tule](https://hashnode.com/@hrishi312)

[Mar 26, 2023](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/641ff001be4ca15b2ad2ab0c)

beautifully explained the topic. thank you

[Ayesha Sahar](https://hashnode.com/@IAyeshaSahar)

[Mar 26, 2023](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/641ff001be4ca15b2ad2ab0c#reply-6420835fbdbe7d697133c3a4)

Glad that you liked it!

[Nirmal Shah](https://hashnode.com/@saveaspng)

[Aug 14, 2022](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/62f86e55fffdab933bde90a9)

What I fail to understand is, even if the console is a closure of setTimeout, how does the value of j become 4 if the condition we set in the for loop is that j <=3?

[Nirmal Shah](https://hashnode.com/@saveaspng)

[Aug 14, 2022](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/62f86e55fffdab933bde90a9#reply-62f8e62e69fa1793160c565f)

And it won't even execute the inner block those three times just because "j" is a var? [Ayesha Sahar](https://hashnode.com/@IAyeshaSahar)

[Ayesha Sahar](https://hashnode.com/@IAyeshaSahar)

[Aug 14, 2022](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/62f86e55fffdab933bde90a9#reply-62f8e7acfffdab933bde9fb0)

The main function is only executed thrice and we get the answer accordingly. What is happening is that since it's a closure, it will store the value after j<= 3, which is 4, when it is stopping. Hence, that value will be stored in j and only that will be accessible, that's why we see 4. In order to avoid this case, where we only get the value of the end, we use the solutions mentioned! [Nirmal Shah](https://hashnode.com/@saveaspng)

[Nirmal Shah](https://hashnode.com/@saveaspng)

[Aug 14, 2022](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/62f86e55fffdab933bde90a9#reply-62f8ecc7fffdab933bdea08f)

Got it, thank you! [Ayesha Sahar](https://hashnode.com/@IAyeshaSahar)

[Archie Andrews](https://hashnode.com/@salmanjs)

[Aug 3, 2022](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/62ea4d5016f9480a9dac549d)

As always great article! It was well explained..

[Ayesha Sahar](https://hashnode.com/@IAyeshaSahar)

[Aug 3, 2022](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/62ea4d5016f9480a9dac549d#reply-62ea574892b7fd0a78c35ffd)

Thank you so much🤗

[Devang](https://hashnode.com/@devangpatil)

[Aug 2, 2022](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/62e90bb83f084d032011d9de)

index is not defined in Closures in a loop. It will throw an error.

[Ayesha Sahar](https://hashnode.com/@IAyeshaSahar)

[Aug 2, 2022](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/62e90bb83f084d032011d9de#reply-62e90d4f3f084d032011da1b)

Sorry for the trouble! Thanks for telling me. I've updated the code :)
