How to Find the Sum of an Array of Numbers in JavaScript

  1516 views   2 years ago Javascript

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:

    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


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]