How to Loop Through an Array in JavaScript?
Last Updated :
15 Apr, 2025
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.
JavaScript
const a = [1, 2, 3, 4, 5];
for (let i = 0; i < a.length; i++) {
console.log(a[i]);
}
- 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.
JavaScript
const a = [1, 2, 3, 4, 5];
a.forEach((ele) => {
console.log(ele);
});
- 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.
JavaScript
const a1 = [1, 2, 3, 4, 5];
const a2 = a1.map((ele) => {
return ele * 2;
});
console.log(a2);
- 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.
JavaScript
const a = [1, 2, 3, 4, 5];
for (const ele of a) {
console.log(ele);
}
- 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.
JavaScript
const a = [1, 2, 3, 4, 5];
for (const i in a) {
console.log(a[i]);
}
- 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.
JavaScript
const a = [1, 2, 3, 4, 5];
const s = a.reduce((acc, cVal) => {
return acc + cVal;
}, 0);
console.log(s);
- 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. |
Similar Reads
How to Loop Through Array in jQuery? jQuery offers various methods for looping through arrays, enabling operations like data transformation, filtering, and manipulation. Efficient array iteration is crucial for dynamic web applications and interactive content management.Below are the approaches to loop through an array in JQuery:Table
5 min read
How to Find the Length of an Array in JavaScript ? JavaScript provides us with several approaches to count the elements within an array. The length of an array lets the developer know whether an element is present in an array or not which helps to manipulate or iterate through each element of the array to perform some operation. Table of Content Usi
3 min read
How to Get the Size of an Array in JavaScript To get the size (or length) of an array in JavaScript, we can use array.length property. The size of array refers to the number of elements present in that array. Syntaxconst a = [ 10, 20, 30, 40, 50 ] let s = a.length; // s => 5 The JavaScript Array Length returns an unsigned integer value that
2 min read
How to view array of a structure in JavaScript ? The Javascript arrays are heterogeneous. The structure of the array is the same which is enclosed between two square brackets [ ], and the string should be enclosed between either "double quotes" or 'single quotes'. You can get the structure of by using JSON.stringify() or not, here you will see the
3 min read
How to Push an Array into Object in JavaScript? To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.Understanding the push() MethodThe array push() method adds one
2 min read
What is the fastest way to loop through an array in JavaScript ? The fastest way to loop through an array in JavaScript depends upon the usecases and requirements. JavaScript has a large variety of loops available for performing iterations. The following loops along with their syntax are supported by JavaScript. Table of Content for loop while loop .forEach() loo
3 min read
How to Access Array of Objects in JavaScript ? Accessing an array of objects in JavaScript is a common task that involves retrieving and manipulating data stored within each object. This is essential when working with structured data, allowing developers to easily extract, update, or process information from multiple objects within an array.How
4 min read
How to Add the Numbers in a JavaScript Array? Adding the numbers in a JavaScript array is a common operation, particularly in tasks involving data aggregation or mathematical calculations. This process involves iterating through the array and summing its elements. JavaScript provides multiple methods to achieve this, ranging from traditional lo
2 min read
How to Filter an Array in JavaScript ? The array.filter() method is used to filter array in JavaScript. The filter() method iterates and check every element for the given condition and returns a new array with the filtered output.Syntaxconst filteredArray = array.filter( callbackFunction ( element [, index [, array]])[, thisArg]);Note: I
2 min read
How To Iterate an Array using forEach Loop in JavaScript? Iterating through an array in JavaScript can be done in multiple ways. One of the most common methods is using the traditional for loop. The modern approach to iterate the array which is by using the forEach method. This method is different from the traditional approach as it uses a functional appro
1 min read