Array.prototype.forEach()

Executes a provided function once for each array element.

Since ES5 Spec ↗

Syntax

arr.forEach(callback, thisArg)

Parameters

NameTypeRequiredDescription
callback Function Yes Function called for each element with (element, index, array). Its return value is ignored.
thisArg any No Value to use as `this` when executing the callback.

Returns

undefined — Always returns undefined.

Examples

['a', 'b', 'c'].forEach((v, i) => console.log(i, v));
Output
0 a
1 b
2 c
let sum = 0;
[1, 2, 3].forEach(n => { sum += n; });
console.log(sum);
Output
6

Notes

Cannot be stopped early with `break` or `return`; use a `for...of` loop or `some`/`every` for early exit. Skips empty slots in sparse arrays. Does not build a new array - use `map` for that.

Browser & runtime support

EnvironmentSince version
chrome 1.0
firefox 1.5
safari 3.0
edge 12
node 0.10

See also