Array forEach()
Use Array forEach() to iterate over the array items and call the function for each item.
Syntax
array.forEach(function callback(arrayItem, index, array), thisValue);array - The array to iterate over.
Arguments
arrayItem
Next array item to be processed by the function.
index
Index of the current arrayItem being processed.
array
The array being iterated over.
thisValue
Item to represent the this value.
Return Value
This function always returns undefined.
Notes
- The array is not mutated.
- Adding items to the array after iterations have begun, will not be included in the processing.
- Changing the array items during processing will not change the item value passed into the function; the original value is used.
- forEach() cannot be chained.
Example
const letters = ['a', 'b', 'c'];
letters.forEach((letter, index, arr) => {
console.log(letter, index, arr);
});
// The console will output
// 'a', 0, ['a', 'b', 'c']
// 'b', 1, ['a', 'b', 'c']
// 'c', 2, ['a', 'b', 'c']