W3docs

How to Loop through an Array in JavaScript

The most common ways to loop through an array in JavaScript are the for, for/in and while loops. See how to use them. Examples.

Looping through an array JavaScript is among the most frequent things a programmer will do. An array can include many data entries, and you may wish to do a task on all the data entries. For example, you have an array of numbers you wish to calculate the summation of them, or you have an array of objects and you want to add a property to all of these objects. There are several ways to iterate over an array in JavaScript. Let’s have a look and find the optimal one for you.

for loop

The classic and famous <kbd class="highlighted">for</kbd> loop iterates over any custom range of numbers you specify and runs a block of code on each iteration. Whenever you want to iterate over an array, a straightforward way is to have a for loop iterating over the array's indices, which means iterating from zero to the length of the array.

Javascript array for loop

javascript— editable

The for loop uses 3 expressions:

  1. Initialization - initializes the loop variable with a starting value which can only be executed once.
  2. Condition - specifies the situation under which the loop should be stopped.
  3. Final expression - is performed at the end of each loop execution. It is used to increment the index.

for...in

The <kbd class="highlighted">for...in</kbd> loops through the properties of an object. It is among the most used and straightforward methods for iterating over objects. You define a variable like <kbd class="highlighted">i</kbd> in <kbd class="highlighted">for (let i in arr)</kbd>, and in each iteration, the variable <kbd class="highlighted">i</kbd> will become equal to a key in <kbd class="highlighted">arr</kbd>. Then you will be able to select the desired array element with something like <kbd class="highlighted">arr[i]</kbd>. See an example below.

Note: Using for...in on arrays is generally discouraged. It iterates over enumerable string keys (including inherited ones) and does not guarantee iteration order. It is better suited for plain objects.

Javascript array for..in loop

javascript— editable

for...of

If you want to access the values inside an array directly, and not the keys, then you should use <kbd class="highlighted">for...of</kbd> instead of <kbd class="highlighted">for...in</kbd>.

Javascript for...of loop

javascript— editable

Using for...of with entries() method

This one is a bit complex. Every array has a method <kbd class="highlighted">entries()</kbd> which returns an iterable of both keys and values of an array. using <kbd class="highlighted">for...of</kbd> with this method enables us to have an iteration over both keys and values.

Javascript for...of with entries()

javascript— editable

while

The <kbd class="highlighted">while</kbd> loops through a block of code as long as a specified condition is true:

How to Loop through an Array in JavaScript

javascript— editable

In the given example, the code in the loop will run over and over again, as long as the index <kbd class="highlighted">i</kbd> is less than the array's length.

You can use break and continue in a while loop. But when you use the while loop you should take into account the increment for the next iteration. If you do not, then it may result in an infinite loop.

forEach()

An alternative to for and for/in loops is <kbd class="highlighted">Array.prototype.forEach()</kbd>. The <kbd class="highlighted">forEach()</kbd> runs a function on each indexed element in an array. Starting at index[0] a function will get called on index[0], index[1], index[2], etc… <kbd class="highlighted">forEach()</kbd> will let you loop through an array nearly the same way as a for loop:

Javascript while loop

javascript— editable

As you saw, the <kbd class="highlighted">forEach()</kbd> method takes a function as an input argument, and applies the function to each element of the array in a sequential way. Also note that the function it takes as the input parameter is not supposed to return a value, thus cannot be regarded as a pure function. Unlike traditional loops, forEach() is a functional array method rather than a control flow statement.

map()

The map() method creates a new array with the results of calling a function for each array element. In the example below, we define an array named <kbd class="highlighted">myFirstArray</kbd>, and then multiply each element by 2 and store the result in a new array named mySecondArray. Try the code and you will notice that the first array remains unchanged.

How to Loop through an Array in JavaScript

javascript— editable

At first sight, the <kbd class="highlighted">map()</kbd> method has similarities with the <kbd class="highlighted">forEach()</kbd> method since it will also invoke the callback function once for each array element. The difference is that <kbd class="highlighted">map()</kbd> creates and returns new arrays based on the callback function result. Like forEach(), map() is a functional array method, not a traditional loop construct.