Open In App

How to use loop through an array in JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to use the various types of loops available in JavaScript with arrays. In this article, we will see the implementation of forEach(), for..of, and for loops.

We can use loop through an array in JavaScript in the following ways:

Method 1: Using the forEach() method

The forEach() method is used to execute code for each element in the array by looping through each of them. 

Syntax:

array.forEach( arrayElement => {
// Lines of code to execute
});

Example: In this example, we will loop through an array using the forEach() method and print the values.

Javascript




function loopArray() {
    let arr = ['Item 1', 'Item 2', 'Item 3',
        'Item 4', 'Item 5'];
 
    arr.forEach(element => {
       console.log(element);
    });
}
 
loopArray();


Output

Item 1
Item 2
Item 3
Item 4
Item 5


Method 2: Using the for…of statement

The for…of statement can be used to loop through iterable objects and perform the required functions. Iterable objects include arrays, strings, and other array-like objects. 

Syntax:

for (arrayElement of array) {
// Lines of code to execute
}

Example: In this example, we will loop through an array using the for..of statement and print the values.

Javascript




function loopArray() {
    let arr = ['Item 1', 'Item 2', 'Item 3',
        'Item 4', 'Item 5'];
 
    for (element of arr) {
       console.log(element);
    }
}
 
loopArray();


Output

Item 1
Item 2
Item 3
Item 4
Item 5


Method 3: Using the basic for loop

The default for loop can be used to iterate through the array and each element can be accessed by its respective index. 

Syntax:

for (i = 0; i < list.length; i++) {
// Lines of code to execute
}

Example: In this example, we will loop through an array using the for loop and print the values.

Javascript




function loopArray() {
    let arr = ['Item 1', 'Item 2', 'Item 3',
        'Item 4', 'Item 5'];
 
    for (i = 0; i < arr.length; i++) {
        console.log(arr[i]);
    }
}                          
loopArray();


Output

Item 1
Item 2
Item 3
Item 4
Item 5


Method 4: Using While Loop

A While Loop in JavaScript is a control flow statement that allows the code to be executed repeatedly based on the given boolean condition. The while loop is used to iterate over the array.

Example: In this example, we are using While Loop.

Javascript




function loopArray() {
    let arr = ['Item 1', 'Item 2', 'Item 3',
        'Item 4', 'Item 5'];
    let length = arr.length;
    while (length > 0) {
        console.log(arr[arr.length - length]);
        length--;
    }
}
loopArray();


Output

Item 1
Item 2
Item 3
Item 4
Item 5


Method 5: Using the for…in statement

The for..in statement can be used to loop through iterable objects and we can unveil objects properties one by one accessing each key to explore its hidden values.

Syntax:

for (let index in array) {
// Access the element using array[index]
}

Example: In this example, we will loop through an array using the for..in statement and print the values.

Javascript




function loopArray() {
  let arr = ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'];
 
  for (let index in arr) {
    console.log(arr[index]);  // Access the element using its index
  }
}
 
loopArray();


Output

Item 1
Item 2
Item 3
Item 4
Item 5


Last Updated : 29 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads