OneCompiler

[JavaScript] How to compute sum of all elements from an array?

I want to find the sum of all elements from an array, how can I do that in Javascript?

1 Answer

4 years ago by

Below is the simplest way to find the sum of all elements from an array:

let numbers = [ 1, 2, 3, 4, 5 ];
let sum = numbers.reduce((a, b) => a + b);
console.log("Sum of array elements: ", sum);

Try running the above code online here: https://onecompiler.com/javascript/3xnkfhen9

4 years ago by Meera