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
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read