DEV Community

Mehul Lakhanpal
Mehul Lakhanpal

Posted on • Originally published at codedrops.tech

Find sum of all the elements of an array using reduce()

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

const sum = arr.reduce((previousSum, element) => {
  const newSum = previousSum + element;
  return newSum;
}, 0); // Initially the sum is set to 0

console.log(sum); // 15
Enter fullscreen mode Exit fullscreen mode

Discussion (0)