# Iterate Through an Array with a For Loop

**URL:** <https://forum.freecodecamp.org/t/iterate-through-an-array-with-a-for-loop/569082>\
**Category:** I Built This\
**Created:** [November 5, 2022, 3:37pm UTC](https://forum.freecodecamp.org/t/iterate-through-an-array-with-a-for-loop/569082 "2022-11-05T15:37:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![enda22](https://avatars.discourse-cdn.com/v4/letter/e/8baadc/32.png) [@enda22](https://forum.freecodecamp.org/u/enda22)\
**Post date:** [November 5, 2022, 3:37pm UTC](https://forum.freecodecamp.org/t/iterate-through-an-array-with-a-for-loop/569082/1 "2022-11-05T15:37:13Z")

</div>

Hi I’ve got the right code for this question.

The links here.

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop)**
>
> Learn to Code — For Free

and my code is right

```auto
// Setup

const myArr = [2, 3, 4, 5, 6];

// Only change code below this line

var total = 0

for (var i = 0; i < myArr.length; i++) {

total += myArr[i];

}

```

However, I am just confused as to how the code "total += myArr[i] can equate to 20 in the consolelog.

Can anyone kindly explain this?  
console.log(total)

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [November 5, 2022, 3:41pm UTC](https://forum.freecodecamp.org/t/iterate-through-an-array-with-a-for-loop/569082/2 "2022-11-05T15:41:28Z")

</div>

total starts out as 0  
total = 0  
Then the for loop runs from i = 0 to i = length of the array less 1  
within the for loop the total is getting incremented each time by the value of the current number myArr[i]  
So a table showing what happens may show  
value of total  
0  
0 + 2  
2 + 3  
5 + 4  
9 + 5  
14 + 6  
20

---

<div class="post-metadata">

**Author:** ![enda22](https://avatars.discourse-cdn.com/v4/letter/e/8baadc/32.png) [@enda22](https://forum.freecodecamp.org/u/enda22)\
**Post date:** [November 5, 2022, 4:10pm UTC](https://forum.freecodecamp.org/t/iterate-through-an-array-with-a-for-loop/569082/3 "2022-11-05T16:10:40Z")

</div>

thank you so much that explains it perfectly.

the console log is also showing if i made i = 3. that the value console log would output 11. can you quickly explain that if you dont mind?

i really appreciate all your help.

Just starting my coding journey.

---

<div class="post-metadata">

**Author:** ![hbar1st](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hbar1st/32/372649_2.png) [@hbar1st](https://forum.freecodecamp.org/u/hbar1st)\
**Post date:** [November 5, 2022, 4:19pm UTC](https://forum.freecodecamp.org/t/iterate-through-an-array-with-a-for-loop/569082/4 "2022-11-05T16:19:40Z")

</div>

if i starts from 3 instead of 0 then  
total is 0 again at the start  
but we are adding  
0 + myArr[3] and so we get 0 + 5  
5 + 6  
11

---

<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:** [November 5, 2022, 4:19pm UTC](https://forum.freecodecamp.org/t/iterate-through-an-array-with-a-for-loop/569082/5 "2022-11-05T16:19:54Z")

</div>

Can you try walking through what you think happens if i starts at 3?

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [May 7, 2023, 4:20am UTC](https://forum.freecodecamp.org/t/iterate-through-an-array-with-a-for-loop/569082/6 "2023-05-07T04:20:16Z")

</div>

Closed on May 7, 2023, 4:20am UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
