# Basic JavaScript - Iterate Through an Array with a For Loop

**URL:** <https://forum.freecodecamp.org/t/basic-javascript-iterate-through-an-array-with-a-for-loop/530118>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [July 16, 2022, 7:30pm UTC](https://forum.freecodecamp.org/t/basic-javascript-iterate-through-an-array-with-a-for-loop/530118 "2022-07-16T19:30:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![visurex](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/visurex/32/240074_2.png) [@visurex](https://forum.freecodecamp.org/u/visurex)\
**Post date:** [July 16, 2022, 7:30pm UTC](https://forum.freecodecamp.org/t/basic-javascript-iterate-through-an-array-with-a-for-loop/530118/1 "2022-07-16T19:30:09Z")

</div>

so yeah why does it count up the index numbers of the array,  
and not just count the indexes of the array ?  
5 indexes that count up 20 ?

and why as usual is that use for ?

**Your code so far**

```js
// 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 = total + myArr[i];
}

```

**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.114 Safari/537.36 Edg/103.0.1264.62`

**Challenge:** Basic JavaScript - Iterate Through an Array with a For Loop

**Link to the challenge:**

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

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [July 16, 2022, 8:31pm UTC](https://forum.freecodecamp.org/t/basic-javascript-iterate-through-an-array-with-a-for-loop/530118/2 "2022-07-16T20:31:00Z")

</div>

> [@visurex](#):
>
> `myArr[i]`

If `i=0` what is this value? If `i=1` what is this value? Keep going up to `i=4`. Do you see what it is adding to `total` each time?

---

<div class="post-metadata">

**Author:** ![zyker.tompos](https://avatars.discourse-cdn.com/v4/letter/z/50afbb/32.png) [@zyker.tompos](https://forum.freecodecamp.org/u/zyker.tompos)\
**Post date:** [July 16, 2022, 8:42pm UTC](https://forum.freecodecamp.org/t/basic-javascript-iterate-through-an-array-with-a-for-loop/530118/3 "2022-07-16T20:42:28Z")

</div>

I will try to clear things up.

> [@visurex](#):
>
> so yeah why does it count up the index numbers of the array,  
> and not just count the indexes of the array ?

A for loop can be easily pulled apart into a while loop to explain things.

```auto
for (let i = 0 /*initialization*/; i<10 /*condition*/; i++/*increment/progress*/){
  total += i;
}

/* A for loop rewritten as a while loop */

let i = 0; /*initalizaiton*/
while (i<10 /*condition*/){
  i++ //increment/progression
}

```

Here are all the parts explained

1. Initialization: Used for initalizaing a counter, usually i, to track how many loops you have completed. It is also completed only **once** before your loop happens (like the while loop example).

2. Condition: A boolean expression that equates to true or false. If it is true, it will run a loop, otherwise, it will break out.

3. Increment / Progress: Online, it is called an increment or decrement, but it can really be any operation, even multiplication and division. It just represents that your count variable is ‘progressing’ through your loop. This causes your counter to change, breaking the condition and exiting the loop.

A for loop is basically a while loop compressed, repeating a piece of code ‘while’ the condition is true. So what your telling your code up above to do is to access indexes 0, 1, 2, 3, 4 (the condition is the array’s length, so it will do all of them) values in the array and then it will add them to total. Because total is global, it won’t get reset when the loop resets, so your outcome is the elements added together.

> [@visurex](#):
>
> why as usual is that use for ?

A while loop is generally used for unknown amount of repeating (game loops, file searching, etc).  
A for loop is used generally for repeating a certain amount of times before (testing a function 5 times) or during runtime (array.length: this can change throughout runtime)

One of the main uses is what you are doing now, going through each element in an array.

Hope this has been helpful!

---

<div class="post-metadata">

**Author:** ![visurex](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/visurex/32/240074_2.png) [@visurex](https://forum.freecodecamp.org/u/visurex)\
**Post date:** [July 16, 2022, 8:47pm UTC](https://forum.freecodecamp.org/t/basic-javascript-iterate-through-an-array-with-a-for-loop/530118/4 "2022-07-16T20:47:38Z")

</div>

yeah it did thanks 🙂

---

<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:** [January 15, 2023, 8:47am UTC](https://forum.freecodecamp.org/t/basic-javascript-iterate-through-an-array-with-a-for-loop/530118/5 "2023-01-15T08:47:41Z")

</div>

Closed on January 15, 2023, 8:47am UTC

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