Array.prototype.forEach()

Baseline Widely available

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.

Try it

const array = ["a", "b", "c"];

array.forEach((element) => console.log(element));

// Expected output: "a"
// Expected output: "b"
// Expected output: "c"

Syntax

js
forEach(callbackFn)
forEach(callbackFn, thisArg)

Parameters

callbackFn

A function to execute for each element in the array. Its return value is discarded. The function is called with the following arguments:

element

The current element being processed in the array.

index

The index of the current element being processed in the array.

array

The array forEach() was called upon.

thisArg Optional

A value to use as this when executing callbackFn. See iterative methods.