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
andb
- Initialize two variable with value of string
a = [3, 5, 7, 4]
andb = 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