Favorites

JavaScript Array.prototype.forEach() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Array.prototype.forEach()
JavaScript Array Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

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, index and array.
  • thisArgOptional. A value to use as this inside callbackFn.

Return Value

None (undefined).

Example

const fruits = ['apple', 'banana', 'cherry'];
fruits.forEach((fruit, i) => {
  console.log(i + 1 + '. ' + fruit);
});

let total = 0;
[5, 10, 15].forEach(n => { total += n; });
console.log(total);

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.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee