Calls a function for each element in fruits:
const fruits = ["apple", "orange", "cherry"];
fruits.forEach(myFunction);
The forEach()
method calls a function for each element in an array.
The forEach()
method is not executed for empty elements.
array.forEach(function(currentValue, index, arr), thisValue)
Parameters function() Required.undefined
.
this
value. Return Value More Examples
Compute the sum:
let sum = 0;
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction);
function myFunction(item) {
sum += item;
}
Try it Yourself »Multiply each element:
const numbers = [65, 44, 12, 4];
numbers.forEach(myFunction)
function myFunction(item, index, arr) {
arr[index] = item * 10;
}
Try it Yourself » Browser SupportforEach()
is an ECMAScript5 (ES5 2009) feature.
JavaScript 2009 is supported in all browsers since July 2013:
ChromeTrack your progress - it's free!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4