How To Loop Through An Array In JavaScript

To loop through an array in JavaScript, you can use any of these methods –

  1. Use sequential for loop – A for loop is used to execute a set of statements repeatedly until the condition becomes false.
  2. Use for-of loop – A for-of loop makes it easier to iterate over arrays and other iterable objects.
  3. Use do-while loop – A do…while loop is similar to a while loop, except that a do…while loop is guaranteed to execute at least one time.
  4. Use while loop – A while loop repeats a set of statements until the condition becomes false.
  5. Use Array.forEach() method – The forEach() method executes a provided function once per array element.

Let’s look at these methods in detail below.

how to loop through an array in javascript

Use Sequential for Loop To Loop Through An Array In JavaScript

A sequential for loop is the most basic type of loop. It has the following syntax −

for(initialization; condition; increment) {

// Statements inside the body of loop

}

The initialization statement initializes the loop; it’s executed once, when the loop starts. The condition statement evaluates the condition; if it’s true, the loop body is executed. If it’s false, the loop stops. The increment statement increments or decrements your loop counter.

The for loop has the following three parts −

Initialization − You initialize variables used in the loop (e.g., i = 0)

Condition − You define the condition for running the loop (e.g., i must be less than 5). If the condition evaluates to true, the loop body is executed. If the condition evaluates to false, the loop stops executing.

Increment/decrement − This statement increments or decrements your loop counter (e.g., i++) after each iteration through the loop.

Here’s an example of how to use a for loop to print all elements of an array –

var arr = [1,2,3,4,5];

for(i = 0; i < arr.length; i++) {

console.log(arr[i]);

}

Output

1

2

3

4

5

Use for-of Loop To Loop Through An Array In JavaScript

ES6 introduces the for-of loop, which makes it easier to iterate over arrays and other iterable objects –

var arr = ['a','b','c'];

for(var val of arr) {

console.log(val);

}

Output

a

b

c

Use do-while Loop To Loop Through An Array In JavaScript

A do-while loop is similar to a while loop, except that a do…while loop is guaranteed to execute at least one time.

The syntax of a do-while loop is as follows −

Syntax

do {

// Statements

} while(condition);

Here’s an example of how to use a do-while loop to print all elements of an array –

var arr = ['a','b','c'];

var i = 0;

do {

console.log(arr[i]);

i++;

} while (i<arr.length)

Output

a

b

c

Use while Loop To Loop Through An Array In JavaScript

A while loop is used to execute a set of statements repeatedly until the condition becomes false. The syntax of a while loop is as follows −

Syntax

while(condition) {

// Statements

}

Here’s an example of how to use a while loop to print all elements of an array –

var arr = ['a','b','c'];

var i = 0;

while(i < arr.length) {

console.log(arr[i]);

i++;

}

Output

a

b

c

Use Array.forEach() Method

The forEach() method executes a provided function once per array element.

The syntax of the forEach() method is as follows −

array.forEach(function(currentValue, index, arr), thisValue)

Here’s an example of how to use the forEach() method to print all elements of an array –

var arr = ['a','b','c'];

arr.forEach(function(element) {

console.log(element);

});

Output

a

b

c

Conclusion

In this article, we looked at how to loop through an array in JavaScript. We saw how to use for loop, for-of loop, while loop, do-while loop and forEach() method to loop through an array. Choose the method depending on your requirement.

I hope you found this article helpful. Thanks for reading!

Leave a Reply