DEV Community

Discussion on: Closures in JavaScript

Collapse
 
endeavor42 profile image
Fitri

Hi, great article!
I really liked your last example and decided to code it myself. After executing it, I got a bunch of sixes, as expected. However, when I declared the variable i using the β€œlet” keyword, I got 0-5. Why is that happening? I thought i has a value of 6 at the time of invocation.

Collapse
 
bhagatparwinder profile image
Parwinder πŸ‘¨πŸ»β€πŸ’»

Thanks for reading and bringing up the confusion. This happened due to my copy paste job from my notes. I have corrected it to var.

As a bonus, I have updated the blog post with an explanation of why let solves this problem as well (similar to closure + IIFE). This should answer the question you have πŸ˜… If you still have any more questions please feel free to follow up.

Collapse
 
endeavor42 profile image
Fitri

Thank you for the quick response! Your explanation answered my question πŸ˜„