Loop (for each) over an array in JavaScript
Categories:
Mastering Array Iteration: The 'for...of' Loop in JavaScript

Explore various methods for iterating over arrays in JavaScript, focusing on the modern and efficient 'for...of' loop, along with traditional and functional approaches.
Iterating over arrays is a fundamental operation in JavaScript programming. Whether you need to process each element, transform data, or perform calculations, understanding the different looping constructs is crucial. This article delves into the most common and effective ways to loop through arrays, with a special emphasis on the for...of
loop, which offers a clean and readable syntax for iterating over iterable objects.
The 'for...of' Loop: Modern Iteration
Introduced in ECMAScript 2015 (ES6), the for...of
loop provides a straightforward way to iterate over the values of iterable objects, including arrays, strings, Maps, Sets, and more. It directly accesses the value of each element, making the code cleaner and less error-prone compared to traditional for
loops when you only need the element's value.
const fruits = ['apple', 'banana', 'cherry'];
for (const fruit of fruits) {
console.log(fruit);
}
// Output:
// apple
// banana
// cherry
Basic usage of the for...of
loop with an array.
for...of
loop is ideal when you need to iterate over the values of an iterable. If you need the index of each element, you can combine it with Array.prototype.entries()
.const colors = ['red', 'green', 'blue'];
for (const [index, color] of colors.entries()) {
console.log(`Index: ${index}, Color: ${color}`);
}
// Output:
// Index: 0, Color: red
// Index: 1, Color: green
// Index: 2, Color: blue
Using for...of
with entries()
to get both index and value.
Traditional Loops: 'for' and 'forEach()'
Before for...of
, the traditional for
loop and the Array.prototype.forEach()
method were the primary ways to iterate over arrays. While for...of
often provides a more concise syntax, understanding these methods is still important for working with older codebases or when specific requirements (like early exit from a loop) dictate their use.
flowchart TD A[Start Iteration] --> B{Choose Method} B -->|Need Index & Control| C[Traditional 'for' loop] B -->|Need Value & Simple Iteration| D['for...of' loop] B -->|Functional Approach| E['forEach()' method] C --> F[Access element by index] D --> G[Directly access element value] E --> H[Callback function for each element] F --> I[End Iteration] G --> I H --> I
Decision flow for choosing an array iteration method.
// Traditional 'for' loop
const numbers = [10, 20, 30];
for (let i = 0; i < numbers.length; i++) {
console.log(`Element at index ${i}: ${numbers[i]}`);
}
// Array.prototype.forEach()
const names = ['Alice', 'Bob', 'Charlie'];
names.forEach(function(name, index) {
console.log(`Name ${index + 1}: ${name}`);
});
Examples of traditional for
loop and forEach()
.
forEach()
method does not allow you to break
or continue
out of the loop. If you need to stop iteration early based on a condition, use a for
loop, for...of
loop, or methods like some()
or every()
.Performance Considerations and Best Practices
While modern JavaScript engines optimize various loop types, the choice between them often comes down to readability, maintainability, and specific functional requirements rather than raw performance in most typical scenarios. For very large arrays or performance-critical applications, micro-benchmarking might be necessary, but generally, choose the loop that best expresses your intent.

Feature comparison of common array iteration methods.
Here's a summary of when to use each loop type:
1. for...of
Loop
Use when you need to iterate over the values of an array (or any iterable) and don't require the index, or when you can combine it with entries()
for index access. It's generally the most readable for simple value iteration.
2. Traditional for
Loop
Use when you need fine-grained control over the iteration, such as iterating backwards, skipping elements, or breaking out of the loop early. It's also useful when working with array-like objects that are not iterable.
3. forEach()
Method
Use for simple iteration where you want to execute a function for each element and do not need to break out of the loop. It's a functional approach that can make code more declarative.
4. Other Array Methods
Consider map()
, filter()
, reduce()
, some()
, and every()
for specific transformations, filtering, or aggregation tasks. These methods are often more expressive and lead to cleaner code than manual loops for their respective purposes.