8.5 Ways to Loop Through an Array in JavaScript
By: David Fekke
Tags: Arrays, JavaScript, Loop

From my blog post on JavaScript loops.

One of my favorite expressions is “there is more than one way to skin a cat”. Don’t get me wrong, I love cats, but this will illustrate my point.

Photo by Linnea Sandbakk on Unsplash

JavaScript as a language gives you many different ways of accomplishing the same task. Iterating or looping through an array is an example of something where you have many different ways to accomplish same task.

For this example let’s create an array with five elements in it using an array literal. We will also create a simple function for returning the results to a console.

1. Conditional for statement

The original way of looping through an array in JavaScript is using the traditional ‘for’ loop statement which takes three expressions followed by the statement.

The first part of the for loop in parenthesis defines an initalizer. This lets the for loop keep a variable to track where it is in the loop. The second part is the condition. Here we are telling the loop we only want to loop through this array until we reach the end. The last part is the final expression where we increment one to our intializer. This will be incremented each time the statement is executed.

This example will return the following values in this order to the console;

First 2 3.3 Foo Bar

2. For-in loop

The next way of looping through an array is the for-in loop.

In this second example we just iterate through the array. The variable before the ‘in’ keyword will assign an index value for each item in the array. So if there are five items in our array, it will assign the numbers ‘0, 1, 2, 3, 4’ to the ‘index’. We can then pass this value in the subscript of the array to get the value for that position in the array.

3. For-of loop

The for-of loop is similar to the for-in loop, but it will actually return the value of each item in the array.

As we can see in this example above, the ‘item’ variable is assigned the actual value of the array. This same statement can also be used to iterate through all members of an object as well as an array.

4. Do-while loop

The Do-while loop is similar to the original for loop in example 1, but the condition expression is passed at the end of the statement.

In the forth example above we define an iterator variable ‘i’ before our loop. This is not required to use a do-while loop. We just need a condition expression that can be used to exit out of the loop. In this case we incrementing 1 to the ‘i’ variable every time the statement is executed in the loop, and in the while condition we check to make sure that the ‘i’ value is smaller than the array length.

5. While loop

The while loop is similar to the do-while loop, jut you set the condition expression at the beginning of the statement.

In our fifth example we set a variable ‘j’ to our initial iterator, and increment that value by one in the loop statement. The while condition checks to make sure that the ‘j’ value is smaller than the length of the array.

6. ForEach loop

The ‘forEach’ method on the array prototype was added in version 5 of JavaScript. This is actually an example of a higher-order function, or a function that takes another function as a parameter. these types of parameters are sometimes referred to as a ‘callback’. The callback for the ‘forEach’ method takes a parameter of the actual item we are iterating on in the array, but also takes optional parameters for the index and the whole array.

Here is the simplest way we can use the ‘forEach’ method for looping over an array.

In this sixth example we are passing in array function that prints the current item being iterated over in the array. We can also do it like the following;

7. Recursion

JavaScript at its heart is a functional programming language. In a lot of functional programming languages the way you loop through an array is by using recursion, or using a function that can call itself.

In this seventh example we defined a function called ‘recursive’ that takes the array as a parameter. Inside the function we ‘shift’ or remove one item out of the array, then pass this new version of the array into this same function. We have a condition in the function that checks to make sure the array still has any items left in it to process.

If you want to preserve the original array, you can make a copy of the array by using the spread operator ‘…’ like the following example;

8. Symbol iterator

Yet another way we can loop through our array is using a Symbol.iterator.

In the eighth example we use this Symbol iterator to initialize an iterator object. We can then call the ‘next’ method on the iterator object. We can continue to call this iterator until it’s ‘done’ property returns false.

We are using a do-while statement to check when the ‘done’ property is false to end the execution of this iterator.

8.5. Generators

Generators are similar to the iterator we used before in example eight. Because of this I am only going to call this example 8.5. Generators are defined by using an ‘*’ at the beginning of the function name. I personally do not like the way this is used because if you are coming from a ‘C’ based language this can be confused for a pointer.

In this last example we have defined a generator function called ‘myGenerator’. Generators can either return or yield a value. In this example we call the ‘myGenerator function to create a generator object. The Generator object much like the iterator object has a ‘next’ method that will yield the next value in the array. The generator also has a ‘done’ parameter we can use to tell if the generator has completed execution.

Here we used a do-while loop as the expression for processing the generator.

Summary

As you can see from all of these examples there are multiple ways to skin a cat with JavaScript. Just please don’t skin my cat. ;-)