# Closures in loop

**URL:** <https://forum.freecodecamp.org/t/closures-in-loop/380470>\
**Category:** General\
**Created:** [April 28, 2020, 5:45pm UTC](https://forum.freecodecamp.org/t/closures-in-loop/380470 "2020-04-28T17:45:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mizanmahi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mizanmahi/32/129621_2.png) [@mizanmahi](https://forum.freecodecamp.org/u/mizanmahi)\
**Post date:** [April 28, 2020, 5:45pm UTC](https://forum.freecodecamp.org/t/closures-in-loop/380470/1 "2020-04-28T17:45:31Z")

</div>

Number 1 doesn’t work properly but 2 and 3 works how?

1. const array = [1,2,3,4];  
for(var i=0; i \< array.length; i++) {  
setTimeout(function(){  
console.log('I am at index ’ + array[i])  
}, 3000)  
}

2. const array = [1,2,3,4];  
for(let i=0; i \< array.length; i++) {  
setTimeout(function(){  
console.log('I am at index ’ + array[i])  
}, 3000)  
}

3. const array = [1,2,3,4];  
for(var i=0; i \< array.length; i++) {  
(function(closureI) {  
setTimeout(function(){  
console.log('I am at index ’ + array[closureI])  
}, 3000)  
})(i)  
}

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [April 28, 2020, 5:56pm UTC](https://forum.freecodecamp.org/t/closures-in-loop/380470/2 "2020-04-28T17:56:00Z")

</div>

With the delay, for 1) you are executing the console log when `i=4`, which produces an undefined value.

For 2) each instance of `i` is distinct and thus so are the print statements.

For 3) closures provide a way for the function call to ‘remember’ the state of `i` at that point in the loop iterations.

---

<div class="post-metadata">

**Author:** ![mizanmahi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mizanmahi/32/129621_2.png) [@mizanmahi](https://forum.freecodecamp.org/u/mizanmahi)\
**Post date:** [April 28, 2020, 6:01pm UTC](https://forum.freecodecamp.org/t/closures-in-loop/380470/3 "2020-04-28T18:01:13Z")

</div>

Why i is distinct in 2 just because of it is blocked scope?  
Is function can create closure from both another function scope surrounding it and a block scope surrounding it ?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [April 28, 2020, 6:02pm UTC](https://forum.freecodecamp.org/t/closures-in-loop/380470/4 "2020-04-28T18:02:06Z")

</div>

In 2 `i` is distinct because of `let`.

---

<div class="post-metadata">

**Author:** ![mizanmahi](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mizanmahi/32/129621_2.png) [@mizanmahi](https://forum.freecodecamp.org/u/mizanmahi)\
**Post date:** [April 28, 2020, 6:03pm UTC](https://forum.freecodecamp.org/t/closures-in-loop/380470/5 "2020-04-28T18:03:15Z")

</div>

and let create blocked scope identifiers is that right?

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:56pm UTC](https://forum.freecodecamp.org/t/closures-in-loop/380470/6 "2021-06-01T18:56:20Z")

</div>

Closed on June 1, 2021, 6:56pm UTC


