JavaScript Array.prototype.forEach() Method
The forEach() method runs a function once for each element of an array. It doesn't return anything, so it is used for side effects like logging or updating the page.
Description
The forEach() method calls a function once for each element, in order. It always returns undefined, so it's meant for side effects, such as updating the DOM or adding to a total, rather than building a new array (use map() for that).
There is no way to stop a forEach() loop early except by throwing an error; if you need break, use a for...of loop or some(). It also doesn't wait for promises: an async callback starts each call without waiting for the previous one to finish.
Empty slots in sparse arrays are skipped. Changing the array while looping, such as removing elements, can cause elements to be skipped or visited twice, so loop over a copy if you need to modify the original. Because the return value is always undefined, forEach() can't be chained with other array methods.
Syntax
array.forEach(callbackFn)
array.forEach(callbackFn, thisArg)Parameters
- callbackFnA function called for each element, receiving
element,indexandarray. - thisArgOptional. A value to use as
thisinsidecallbackFn.
Return Value
None (undefined).
Example
Browser Support
The following information will show you the current browser support for the Array.prototype.forEach() method. Hover over a browser icon to see the version that first introduced support for this method.
Desktop





Tablets & Mobile





