JavaScript provides multiple methods to iterate over arrays, ranging from traditional loops to modern iteration techniques. Each method has its specific use cases, depending on the operation to be performed and the need for performance or simplicity.
The for
loop is one of the most basic and widely used methods for iterating over an array. It provides complete control over the iteration process.
const arr = [1, 2, 3, 4, 5];
for (let i = 0; i < arr.length; i++) {
console.log(arr[i]);
}
Use when you need maximum control over the iteration, such as skipping elements or iterating backward.
The for...of
loop is a modern and concise way to iterate over arrays. It directly provides the values of the array elements.
const arr = [1, 2, 3, 4, 5];
for (const value of arr) {
console.log(value);
}
Use for simple iterations where only the array values are needed.
The forEach()
method is a higher-order function that executes a provided function once for each array element.
const arr = [1, 2, 3, 4, 5];
arr.forEach((value, index) => {
console.log(`Index: ${index}, Value: ${value}`);
});
Use for straightforward operations on each element without breaking the loop or returning values.
The map()
method creates a new array by applying a provided function to each element of the array.
const arr = [1, 2, 3, 4, 5];
const squared = arr.map((value) => value * value);
console.log(squared); // [1, 4, 9, 16, 25]
Use when you need to transform each element and return a new array.
The filter()
method creates a new array containing only the elements that satisfy a given condition.
const arr = [1, 2, 3, 4, 5];
const evenNumbers = arr.filter((value) => value % 2 === 0);
console.log(evenNumbers); // [2, 4]
Use when you want to extract elements based on a condition.
The reduce()
method applies a reducer function on each element of the array to reduce it to a single value.
const arr = [1, 2, 3, 4, 5];
const sum = arr.reduce((accumulator, value) => accumulator + value, 0);
console.log(sum); // 15
Use when you need to accumulate or combine values, such as calculating sums or averages.
some()
checks if at least one element satisfies a condition.every()
checks if all elements satisfy a condition.
const arr = [1, 2, 3, 4, 5];
console.log(arr.some((value) => value > 3)); // true
console.log(arr.every((value) => value > 0)); // true
Use for conditional checks on elements.
find()
returns the first element that satisfies a condition.findIndex()
returns the index of the first element that satisfies a condition.
const arr = [1, 2, 3, 4, 5];
console.log(arr.find((value) => value > 3)); // 4
console.log(arr.findIndex((value) => value > 3)); // 3
Use when you need to locate a specific element or its index.
The while
loop iterates as long as a specified condition is true.
const arr = [1, 2, 3, 4, 5];
let i = 0;
while (i < arr.length) {
console.log(arr[i]);
i++;
}
Use for scenarios where the condition for iteration is dynamic.
The do...while
loop executes the block of code at least once before checking the condition.
const arr = [1, 2, 3, 4, 5];
let i = 0;
do {
console.log(arr[i]);
i++;
} while (i < arr.length);
Use when you want to ensure the loop body executes at least once.
keys()
: Iterates over indices.values()
: Iterates over values.entries()
: Iterates over[index, value]
pairs.
const arr = [1, 2, 3];
for (const index of arr.keys()) {
console.log(index); // 0, 1, 2
}
for (const value of arr.values()) {
console.log(value); // 1, 2, 3
}
for (const [index, value] of arr.entries()) {
console.log(index, value); // 0 1, 1 2, 2 3
}
Use for specific iteration requirements like working with indices, values, or both.
Method | Key Features | Returns Value? |
---|---|---|
for |
Full control over iteration | No |
for...of |
Simplified iteration over values | No |
forEach() |
Executes a function for each element | No |
map() |
Transforms elements into a new array | Yes |
filter() |
Filters elements based on a condition | Yes |
reduce() |
Reduces array to a single value | Yes |
some() |
Checks if any element satisfies a condition | Yes (boolean) |
every() |
Checks if all elements satisfy a condition | Yes (boolean) |
find() |
Finds the first element that satisfies a condition | Yes |
findIndex() |
Finds the index of the first matching element | Yes |
while |
Iterates based on dynamic conditions | No |
do...while |
Ensures the body is executed at least once | No |
keys() , values() , entries() |
Iterates over indices, values, or both | Yes |
JavaScript provides numerous ways to iterate over arrays, each suitable for different scenarios. Choose the method that best fits your requirements for readability, performance, and functionality.