Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The forEach() method of Array instances executes a provided function once for each array element.
A function to execute for each element in the array. Its return value is discarded. The function is called with the following arguments:
elementThe current element being processed in the array.
indexThe index of the current element being processed in the array.
arrayThe array forEach() was called upon.
thisArg OptionalA value to use as this when executing callbackFn. See iterative methods.
None (undefined).
The forEach() method is an iterative method. It calls a provided callbackFn function once for each element in an array in ascending-index order. Unlike map(), forEach() always returns undefined and is not chainable. The typical use case is to execute side effects at the end of a chain. Read the iterative methods section for more information about how these methods work in general.
callbackFn is invoked only for array indexes which have assigned values. It is not invoked for empty slots in sparse arrays.
The forEach() method is generic. It only expects the this value to have a length property and integer-keyed properties.
There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool.
Early termination may be accomplished with looping statements like for, for...of, and for...in. Array methods like every(), some(), find(), and findIndex() also stops iteration immediately when further iteration is not necessary.
forEach() expects a synchronous function — it does not wait for promises. Make sure you are aware of the implications while using promises (or async functions) as forEach callbacks.
To run a series of asynchronous operations sequentially or concurrently, see promise composition.
Note: In order to display the content of an array in the console, you can use console.table(), which prints a formatted version of the array.
The following example illustrates an alternative approach, using forEach().
The following code logs a line for each element in an array:
The following (contrived) example updates an object's properties from each entry in the array:
Since the thisArg parameter (this) is provided to forEach(), it is passed to callback each time it's invoked. The callback uses it as its this value.
Note: If passing the callback function used an arrow function expression, the thisArg parameter could be omitted, since all arrow functions lexically bind the this value.
The following code creates a copy of a given object.
There are different ways to create a copy of an object. The following is just one way and is presented to explain how Array.prototype.forEach() works by using Object.* utility functions.
The following example is only here for learning purpose. If you want to flatten an array using built-in methods, you can use Array.prototype.flat().
The array argument is useful if you want to access another element in the array, especially when you don't have an existing variable that refers to the array. The following example first uses filter() to extract the positive values and then uses forEach() to log its neighbors.
The callback function is not invoked for the missing value at index 2.
The forEach() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length.
| ECMAScript® 2027 Language Specification # sec-array.prototype.foreach |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Jul 20, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.