JavaScript Array forEach()
Example
List each item in the array:
const fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The forEach() method calls a function once for each element in an
array, in order.
forEach() is not executed for array elements without values.
Browser Support
forEach() is fully supported in all modern browsers:
| Chrome | IE | Edge | Firefox | Safari | Opera |
| Yes | 9.0 | Yes | Yes | Yes | Yes |
Syntax
array.forEach(function(currentValue, index, arr), thisValue)
Parameter Values
| Parameter | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| function(currentValue, index, arr) | Required. A function to be run for each element in the array. Function arguments:
|
||||||||
| thisValue | Optional. A value to be passed to the function to be used as its "this" value. If this parameter is empty, the value "undefined" will be passed as its "this" value |
Technical Details
| Return Value: | undefined |
|---|---|
| JavaScript Version: | ECMAScript 5 |
More Examples
Example
Compute the sum of all values in an array:
let sum = 0;
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction);
function myFunction(item) {
sum += item;
}
Try it Yourself »
Example
Update the value of each element to 10 times the original value:
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction)
function
myFunction(item, index, arr) {
arr[index] = item * 10;
}
Try it Yourself »

