# AJAX Call inside For loop. Console logs incorrect i value

**URL:** <https://forum.freecodecamp.org/t/ajax-call-inside-for-loop-console-logs-incorrect-i-value/92146>\
**Category:** General\
**Created:** [February 28, 2017, 7:38pm UTC](https://forum.freecodecamp.org/t/ajax-call-inside-for-loop-console-logs-incorrect-i-value/92146 "2017-02-28T19:38:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![justinbarr00](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/justinbarr00/32/30273_2.png) [@justinbarr00](https://forum.freecodecamp.org/u/justinbarr00)\
**Post date:** [February 28, 2017, 7:38pm UTC](https://forum.freecodecamp.org/t/ajax-call-inside-for-loop-console-logs-incorrect-i-value/92146/1 "2017-02-28T19:38:07Z")

</div>

Hello!

I am working on my Twitch API page. If you scroll down to line 58 of my Javascript (the bottom), you will see that I have pulled all of the followers into a array called “followers” (line 41-54). I am trying to loop through and get the status of each and change the image to either a check or X depending on if they are online. At the moment I am working with status0 in the html. I’ll add the others later.

If i try to console.log(i); inside the for loop but outside of the AJAX call I get the correct response: 0 1 2 3 4 5 6 7 8  
When I console.log(i); inside my AJAX success function it shows i as 9.

My code may contain many mistakes because I am new. Any help or tips are appreciated!

> **[TwitchTV API](https://codepen.io/justinbarr00/pen/RKEjWB?editors=0111)**
>
> ...

---

<div class="post-metadata">

**Author:** ![JohnL3](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/johnl3/32/16459_2.png) [@JohnL3](https://forum.freecodecamp.org/u/JohnL3)\
**Post date:** [February 28, 2017, 8:22pm UTC](https://forum.freecodecamp.org/t/ajax-call-inside-for-loop-console-logs-incorrect-i-value/92146/2 "2017-02-28T20:22:41Z")

</div>

havent got a answer but have noticed you have a for loop at line 51 like for(i=0  
this makes it a global i … should use for(let i = 0 … or for(var i = 0  
let would be best as it locks it to the for scope

then at line 58 you have for(i =0 … again so that will be reading your previous i (as far as im aware)  
but also you create a function in this for loop which takes a i as a parameter  
it is recommended that you dont create functions within a for loop

so a lot could be going on with that i … that you are not expecting … will see if i can work out if it is doing anything it shouldnt

---

<div class="post-metadata">

**Author:** ![justinbarr00](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/justinbarr00/32/30273_2.png) [@justinbarr00](https://forum.freecodecamp.org/u/justinbarr00)\
**Post date:** [February 28, 2017, 8:22pm UTC](https://forum.freecodecamp.org/t/ajax-call-inside-for-loop-console-logs-incorrect-i-value/92146/3 "2017-02-28T20:22:57Z")

</div>

Found it online. Needed a “closure.”

---

<div class="post-metadata">

**Author:** ![justinbarr00](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/justinbarr00/32/30273_2.png) [@justinbarr00](https://forum.freecodecamp.org/u/justinbarr00)\
**Post date:** [March 1, 2017, 7:42pm UTC](https://forum.freecodecamp.org/t/ajax-call-inside-for-loop-console-logs-incorrect-i-value/92146/4 "2017-03-01T19:42:19Z")

</div>

Ah thanks for catching that! I forget to put the “var” inside my for loops too often.

Is there a better way to take the data from the array and use it in a function without using a for loop? Or are you saying I should define the function elsewhere and then just call the function instead of laying out the whole thing inside?

Thank you for your help!

---

<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:** [January 16, 2021, 5:59pm UTC](https://forum.freecodecamp.org/t/ajax-call-inside-for-loop-console-logs-incorrect-i-value/92146/5 "2021-01-16T17:59:54Z")

</div>


