Array.forEach

forEach — Executes a provided function once per array element.

Description

Array.forEach(function callback, [object this])

Parameters

Name Description Type Default Optional
callback See callback below. function No
this object Yes
callback

The following parameters is passed to the callback.

value

The value of the current index.

index

The current index.

array

The array.

Examples

Example #1 – forEach example
var array = [2, 4, 8]; array.forEach(function () { console.log(this); });
var arr = [2, 4, 8]; arr.forEach(function (value, index, array) { console.log(value, index, array); });

Passing in a custom object for this.

var array = [2, 4, 8]; array.forEach(function () { console.log(this); }, document.location);

External references