How to get the sum of an array of numbers in JavaScript?

There are three ways to get the sum of an array of numbers:

1. Using the reduce() method.
2. Using the forEach() method.
3. Using the for...of loop.

Get the sum of an array of numbers using the reduce() method

To get the sum of the array of numbers using the reduce() method, follow the following steps:

1. Call the reduce() method on the array whose elements you want to sum.
2. Pass the callback function and an initial value of 0 as the second argument to the reduce() method.
3. On each iteration, the reduce() method will add the accumulator and the current value.
4. On the final iteration, the reduce() method will return the sum.
```let sum;
let numbers = [1, 2, 3, 4, 5];

sum = numbers.reduce((accumulator, currentValue)=>{
return accumulator + currentValue;
}, 0);
console.log(sum); //15```

Note: The accumulator is the final value returned by the reduce() method when it has finished processing the array.

The reduce() method is one of the array iteration methods. It accepts a callback function which is called a reducer and initial value parameter. The reducer takes two parameters, accumulator and current value. On each iteration, the value returned by the callback function is assigned to the accumulator. In the final iteration, the accumulator value is returned by the reduce() method.

accumulator currentValue returned value
1st Iteration 0 1 1
2nd Iteration 1 2 3
3rd Iteration 3 3 6
4th Iteration 6 4 10
5th Iteration 10 5 15

Get the sum of an array of numbers using the forEach() method

You can also use the forEach() method to get the sum of an array of numbers. Follow the below steps to get the sum:

1. Declare the `sum` variable and initialize it to 0.
2. Call the `forEach()` method on the array whose elements you want to sum.
3. Pass the callback function to the forEach() method. Within the callback function, add the `sum` variable and the current array processing element and assign the addition result to the `sum` variable.
```let sum = 0;
let numbers = [1, 2, 3, 4, 5];

numbers.forEach(element => {
sum = sum + element;
});
console.log(sum); //15```

Note: If you want to learn more about the forEach() method, visit How to use forEach() method to loop through JavaScript Array?

Get the sum of an array of numbers using for...of loop

ES6 provides a new loop construct, for...of loop. Using the for...of loop, you can iterate over an array and calculate the sum of array elements.

Follow the following steps to get the sum of the array of numbers:

1. Declare the `sum` variable and initialize it to 0.
2. Use the for...of loop to loop through an array. On each iteration, add the current value and the `sum` variable and assign the result to the `sum` variable.
```let sum = 0;
let numbers = [1, 2, 3, 4, 5];

for(let number of numbers){
sum = sum + number;
}
console.log(sum); //15```

Note: If you want to learn more about the forEach() method, visit How to use ES6 for...of loop in JavaScript?