How to Loop Through an Array in JavaScript?
Here are the various ways to loop through an array in JavaScript
1. Using for Loop
The for loop is one of the most used ways to iterate over an array. It gives you complete control over the loop, including access to the array index, which can be useful when you need to modify elements or perform other operations.
const a = [1, 2, 3, 4, 5];
for (let i = 0; i < a.length; i++) {
console.log(a[i]);
}
Output
1 2 3 4 5
- The for loop starts with an initial value of i = 0 and continues until i is less than the length of the array.
- The loop executes the code inside the block, logging each element of the array using a[i].
- This method is flexible and can be easily customized to access array elements based on conditions.
2. Using forEach() Method
The forEach() method is an array-specific method that allows you to loop through elements. It is often preferred for cleaner syntax, especially when you don’t need to modify the array or manage the index manually.
const a = [1, 2, 3, 4, 5];
a.forEach((ele) => {
console.log(ele);
});
Output
1 2 3 4 5
- The forEach() method takes a callback function that gets called for each element in the array.
- The callback function receives the current element (element) as an argument and executes the code inside its block.
3. Using map() Method
The map() method is similar to forEach(), but it is used when you want to create a new array by transforming each element of the original array. It returns a new array with the results of calling the provided function on every element.
const a1 = [1, 2, 3, 4, 5];
const a2 = a1.map((ele) => {
return ele * 2;
});
console.log(a2);
Output
[ 2, 4, 6, 8, 10 ]
- The map() method applies a function to each element of the array and returns a new array with the results.
- In this example, each element is doubled, and the new array [2, 4, 6, 8, 10] is logged.
4. Using for...of Loop
The for...of loop is a modern way to loop through arrays in JavaScript. It iterates directly over the values of the array, which makes the syntax more concise and easier to understand compared to the traditional for loop.
const a = [1, 2, 3, 4, 5];
for (const ele of a) {
console.log(ele);
}
Output
1 2 3 4 5
- The for...of loop iterates over the values in the array directly, without the need for an index.
- It is perfect for situations where you don’t need to know the index of each element.
5. Using for...in Loop
The for...in loop iterates over the indexes of an array (or the properties of an object). While it can be used for arrays, it’s generally more suitable for objects. It’s important to be cautious when using it for arrays, as it can also iterate over inherited properties.
const a = [1, 2, 3, 4, 5];
for (const i in a) {
console.log(a[i]);
}
Output
1 2 3 4 5
- The for...in loop iterates over the indexes of the array, not the values.
6. Using reduce() Method
The reduce() method is often used when you need to accumulate or reduce the elements of an array into a single value. While it’s not typically used for simple iteration, it can be very powerful when processing elements.
const a = [1, 2, 3, 4, 5];
const s = a.reduce((acc, cVal) => {
return acc + cVal;
}, 0);
console.log(s);
Output
15
- The reduce() method processes each element of the array and reduces them into a single value.
- reduce() is versatile and can be used for a variety of tasks like summing, flattening, or accumulating values.
Which Method to Choose?
Method | When to Use |
---|---|
for loop | When you need full control over the loop, including indices. |
forEach() | When you want a cleaner, functional way to loop through elements. |
map() | When you need to transform the array into a new array. |
for...of loop | When you want a simple, modern syntax without needing indexes. |
for...in loop | Not recommended for arrays, but useful for objects. |
reduce() | When you need to accumulate or reduce array elements into a single value. |