About
forEach is a terminal javascript functional operation that will loop over each element:
- on a array
- or intermediaire functional programming method such as map
Example
The syntax is important, the function is called forEach with a uppercase E, otherwise you get a Unknown function call
Array
['blue', 'green', 'yellow'].forEach((color, index) => console.log(`${index} - ${color}`));
Functional Programming
With map
['a','b','c']
.map(letter => letter.toUpperCase())
.forEach(letter => console.log(letter));
Return and not continue
In a foreach loop, you should use the return function to stop the processing and not continue.