Skip to content

All the For Loops

JavaScript2 min read

All the For Loops

There are several different ways to loop through elements in an array. Most likely, you will see for, forEach, for..in, and for..of. These 4 (pseudo pun intended here) are often confused and that's what we are going to fix 💪

In many older codebases and code examples online, you will see the ubiquitous for loop used to iterate over an array. The typical setup initializes a variable i to 0 (because we want to start with the zeroth element). The second statement defines how many times the loop will run, most often you'll iterate over every element in the array - so i < array.length. And the 3rd statement increasing i on every pass. This allows us to access each element in the array by its index and perform something.

Here, we are just logging the score to the console:

1const scores = [82, 89, 90, 94, 81];
2
3for (let i = 0; i < scores.length; i++) {
4 console.log(scores[i]);
5}
6
7> 82
8> 89
9> 90
10> 94
11> 81

In more modern codebases, however, you will see a mix of forEach, for..in, and for..of. The most common scenario you will encounter is that you want to iterate over every element in the array and execute something using that element. So which do you choose?

Let's start with a more concise version of what the for loop gives us. With for..of, we are able to quickly access each element without having to keep track of the index or worry about incrementing/decrementing the index.

1const scores = [82, 89, 90, 94, 81];
2
3for (const score of scores) {
4 console.log(score);
5}
6
7> 82
8> 89
9> 90
10> 94
11> 81

With forEach, you have access to the index of the current element being iterated over, the element value, and the array itself. Sounds great, right? Well it is, and proponents of the Functional Programming paradigm greatly prefer this method of looping. forEach expects a synchronous function, so be aware of that when using async/await with it. You can’t use await in the body of this kind of loop and you can’t leave a forEach loop early. In for loops, we can use break.

1const scores = [82, 89, 90, 94, 81];
2
3scores.forEach((score) => console.log(score));
4
5> 82
6> 89
7> 90
8> 94
9> 81

The for..in loop is meant to be used with objects, not arrays. So if you use it with an array, you will likely get some unexpected output. This is because for..in is iterating of the enumerable properties of the object (at the end of the day, our arrays are objects).

1const scores = [82, 89, 90, 94, 81];
2
3for (const score in scores) {
4 console.log(score);
5}
6
7> 0
8> 1
9> 2
10> 3
11> 4

Here's a basic rundown of when to use for..of vs for..in:

for..in versus for..of

Now that we have a better idea of what the different types of for loops do, let's dive into some exercises to test our newfound knowledge!

In your browser, open up the developer tools to the console tab and console log the scores that are less than 90 using the classic for loop (not for..in or forEach):

1const scores = [82, 89, 90, 94, 81];
2// for() {} loop goes here

Now, console log the scores that are less than 90 using the forEach loop:

1const scores = [82, 89, 90, 94, 81];
2// forEach loop goes here

Next, console log the scores that are less than 90 using the for..of loop:

1const scores = [82, 89, 90, 94, 81];
2// for..of loop goes here

Lastly, console log the index of the scores that are 90 or greater (≥ 90) using the forEach loop. Hint: the second argument is the index apples.forEach((apple, index) => { console.log(index) }):

1const scores = [82, 89, 90, 94, 81];
2// for loop goes here

Final note on iterating over arrays

Remember when I told you that forEach is expecting a synchronous function? Well we can also use that to our advantage when we don't want to explicitly iterate over an array's elements one-by-one. For longer running loops, you'll see the benefit of using forEach vs reaching for for..of

Let's take our scores and loop over them using forEach. If we pretend that we have some longer running code in there using async/await, you'll notice that the console.log(score) doesn't wait for it. This can be a useful tool to take advantage.

1const scores = [82, 89, 90, 94, 81];
2scores.forEach(async (score) => {
3 await new Promise((resolve) => setTimeout(resolve, 500)); //fake long running code
4 console.log(score);
5});

To contrast, for..of will wait for that longer running code to finish before moving on to our console.log(score):

1const scores = [82, 89, 90, 94, 81];
2for (let score of scores) {
3 await new Promise((resolve) => setTimeout(resolve, 500)); //fake long running code
4 console.log(score);
5}

These are basic examples of running in sequence versus running in parallel. If you're needing to run in sequence, reach for for..of in this case. If you are able to run in parallel (you don't need to wait for that longer running process), try using forEach instead.

For more information on the asynchronous version of for..of, see this post on for await..of 👀

Wanna get better at working with arrays in JS? Check out my latest course 🔥

© 2021 by Jonathan Speek