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

- Using the reduce() method.
- Using the forEach() method.
- Using the for...of loop.

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

- Call the reduce() method on the array whose elements you want to sum.
- Pass the callback function and an initial value of 0 as the second argument to the reduce() method.
- On each iteration, the reduce() method will add the accumulator and the current value.
- 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 |

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

- Declare the
`sum`

variable and initialize it to 0. - Call the
`forEach()`

method on the array whose elements you want to sum. - 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?

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:

- Declare the
`sum`

variable and initialize it to 0. - 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?

- How to find odd numbers in an array using JavaScript?
- How to find even numbers in an array using JavaScript?
- How to check if all values in array are true in JavaScript?
- How to check if all values in array are false in JavaScript?
- How to check if all values in array are null in JavaScript?
- How to remove null values from an array in JavaScript?
- How to remove empty elements from an array in JavaScript?