# Converting a piece of javascript from .forEach to for Loop

**URL:** <https://forum.freecodecamp.org/t/converting-a-piece-of-javascript-from-foreach-to-for-loop/469620>\
**Category:** General\
**Created:** [July 16, 2021, 8:26pm UTC](https://forum.freecodecamp.org/t/converting-a-piece-of-javascript-from-foreach-to-for-loop/469620 "2021-07-16T20:26:17Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![javascriptcoding5678](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@javascriptcoding5678](https://forum.freecodecamp.org/u/javascriptcoding5678)\
**Post date:** [July 16, 2021, 11:53pm UTC](https://forum.freecodecamp.org/t/converting-a-piece-of-javascript-from-foreach-to-for-loop/469620/21 "2021-07-16T23:53:52Z")

</div>

Would it be this instead?

`for (let i = 0; i < coverOpts.length; 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:** [July 16, 2021, 11:58pm UTC](https://forum.freecodecamp.org/t/converting-a-piece-of-javascript-from-foreach-to-for-loop/469620/22 "2021-07-16T23:58:21Z")

</div>

That makes more sense to me.

> [@javascriptcoding5678](#):
>
> `coverOpts.forEach(`

This `forEach` loops over the element in an array, so it makes sense to use the length of this array in your `for` loop version.

---

<div class="post-metadata">

**Author:** ![javascriptcoding5678](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@javascriptcoding5678](https://forum.freecodecamp.org/u/javascriptcoding5678)\
**Post date:** [July 17, 2021, 12:33am UTC](https://forum.freecodecamp.org/t/converting-a-piece-of-javascript-from-foreach-to-for-loop/469620/23 "2021-07-17T00:33:55Z")

</div>

Looking at how my previous codes are written.

**From this:**

```auto
  function initCovers(coverOpts) {
    coverOpts.forEach(function (opts) {
      init(opts);
    });
  }

```

**To this?**  
[https://jsfiddle.net/gyn7x1mp/](https://jsfiddle.net/gyn7x1mp/)

```auto
  function initCovers(coverOpts) {
    for(let i = 0; i < coverOpts.length; i++) {
      init(coverOpts[i]);
    }
  }

```

---

<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 17, 2021, 1:00am UTC](https://forum.freecodecamp.org/t/converting-a-piece-of-javascript-from-foreach-to-for-loop/469620/24 "2021-07-17T01:00:22Z")

</div>

You tell us 🙂 Does the `for` loop work the same as the `forEach` loop? This should be pretty easy to test by running the code.

---

<div class="post-metadata">

**Author:** ![javascriptcoding5678](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@javascriptcoding5678](https://forum.freecodecamp.org/u/javascriptcoding5678)\
**Post date:** [July 17, 2021, 1:04am UTC](https://forum.freecodecamp.org/t/converting-a-piece-of-javascript-from-foreach-to-for-loop/469620/25 "2021-07-17T01:04:54Z")

</div>

Everything works the same as it should.

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [July 17, 2021, 2:38am UTC](https://forum.freecodecamp.org/t/converting-a-piece-of-javascript-from-foreach-to-for-loop/469620/26 "2021-07-17T02:38:06Z")

</div>

It sounds like you could do with some refreshing your knowledge of how `for` loops work. I would recomend looking for a basic JS course. FCC of course offers one.

---

<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, 2022, 2:38pm UTC](https://forum.freecodecamp.org/t/converting-a-piece-of-javascript-from-foreach-to-for-loop/469620/27 "2022-01-15T14:38:11Z")

</div>

Closed on January 15, 2022, 2:38pm UTC

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

[Previous page](https://forum.freecodecamp.org/t/converting-a-piece-of-javascript-from-foreach-to-for-loop/469620.md?page=1)
