10

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

 2 years ago
source link: https://www.laravelcode.com/post/how-to-find-the-sum-of-an-array-of-numbers-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

  1334 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:

<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>
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]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK