0

How to find the sum of an array of numbers in JavaScript?

The best and most recommened way to sum all elements of an array is to use reduce function that executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

const array = [1, 2, 3, 4, 5];

const sum = array.reduce((next, prev) => next + prev, 0);

console.log(sum);
RUN
add comment
Suggest a different solution or add another example