forEach() method calls a function for each element in an array.
Function Proto-type 1:
Array.forEach( function(currentValue, index, arr) { ... } );
Function Proto-type 2: simplified version
Array.forEach( function(currentValue) { ... } );
Example 1) Compute the sum:
let sum = 0; const numbers = [ 65, 44, 12, 4 ]; numbers.forEach( function(v) { sum += v; });
Example 2) Compute the sum:
let sum = 0; const numbers = [ 65, 44, 12, 4 ]; numbers.forEach( myFunction); function myFunction(v) { sum += v; }
Example 3) Multiple each element:
let sum = 0; const numbers = [ 65, 44, 12, 4 ]; numbers.forEach( function(v, idx, arr) { arr[idx] = v * 10; });