How to use JavaScript Array forEach()

Program to use JavaScript Array forEach() method


let a = [3, 5, 7, 4];

let b = 0;
a.forEach((c) => {
  b += c;
});

console.log(b);

Output


19

Steps Description

  1. Declare two variables a and b
  2. Initialize two variable with value of string a = [3, 5, 7, 4] and b = 0
  3. The array foreach() method calls a function for all elements of an array.
  4. The value of b variable is displayed with the help of console.log inbuilt function