---
title: "Comment by Nirmal Shah on \"Simplifying Closures in JavaScript\" | Hashnode"
description: "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"
url: "https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6/comment/62f86e55fffdab933bde90a9"
---

[Discussion](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6)

Comment [Simplifying Closures in JavaScript](https://hashnode.com/posts/simplifying-closures-in-javascript/62e7d4fe053e6bd57842a3a6)

[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?

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

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

It will run three times and will only stop the 4th time when the condition of execution of 3 times ( j<= 3 ) is met. That value was 4. It will remember the value of j from the last iteration of the loop and show 4.

[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)
