How to use JavaScript Array forEach()

Array forEach

On this page

Array forEach() Method

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

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