Using Array.reduce() for Accumulation in Javascript


The reduce method applies a function against an accumulator and each element in the array to reduce it to a single value.

Source Code

let numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce((acc, current) => acc + current, 0);
console.log(sum); // 15
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments