To sum up an array in JavaScript, we use the reduce
method.
For instance, we write
const sum = array.reduce((pv, cv) => pv + cv, 0);
to call array.reduce
with a callback that returns the sum between partial sum pv
and the current value cv
.
We set the initial sum
value to 0 with the 2nd argument.