Syntax
arr.forEach((item, index, array) => { });Examples
Basic forEach()
Running a function for each element, purely for its side effects.
const fruits = ["apple", "banana", "cherry"];
fruits.forEach(fruit => {
console.log(fruit);
});
// apple, banana, cherryUsing Index and Array Parameters
The callback also receives the current index and the full array.
const scores = [85, 92, 78];
scores.forEach((score, index, array) => {
console.log(`Score ${index + 1} of ${array.length}: ${score}`);
});
// Score 1 of 3: 85
// Score 2 of 3: 92
// Score 3 of 3: 78forEach() vs map() - Choosing the Right Tool
A common mistake: using forEach() when you actually need a new array.
// Wrong intent - forEach() returns undefined, this does nothing useful
const numbers = [1, 2, 3];
const result = numbers.forEach(n => n * 2);
console.log(result); // undefined
// Correct - use map() when you need the transformed values back
const doubled = numbers.map(n => n * 2);
console.log(doubled); // [2, 4, 6]Best practices
- Use forEach() only for side effects (logging, DOM updates, pushing to an external array) - never expect a return value from it
- Use map() instead of forEach() whenever you actually need a transformed array back
- Remember you cannot break or continue out of a forEach() loop - use a regular for or for...of loop if you need that control
- Avoid using forEach() with async/await callbacks expecting sequential execution - it does not wait for promises between iterations
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Array Basics
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().
Arrays are ordered, zero-indexed collections that can hold values of any type, including a mix of types. They can be created with array literal syntax [] or the Array constructor. The length property reflects the number of elements, and static methods like Array.isArray(), Array.from(), and Array.of() help create or check arrays.push(), pop(), shift(), unshift()
These four methods add or remove elements from the ends of an array, all mutating the original array in place. push() and pop() work on the end of the array (fast). shift() and unshift() work on the beginning (slower, since remaining elements must be re-indexed).slice() and splice()
slice() returns a shallow copy of a portion of an array as a new array, without modifying the original - useful for extracting a section. splice() modifies the original array in place by removing, replacing, or inserting elements at a specific position, and returns the removed elements.concat() and join()
concat() merges two or more arrays into a new array, without modifying the originals. join() converts all array elements into a single string, separated by a specified delimiter (comma by default) - the reverse operation of String.prototype.split().