Use the JavaScript reduce()
Method
You can use the reduce()
method to find or calculate the sum of an array of numbers.
The reduce()
method executes the specified reducer function on each member of the array resulting in a single output value, as demonstrated in the following example:
<script>
var array = [1, 2, 3, 4, 5];
// Getting sum of numbers
var sum = array.reduce(function(a, b){
return a + b;
}, 0);
console.log(sum); // Prints: 15
</script>