loading...

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

318097 profile image Mehul Lakhanpal Originally published at codedrops.tech ・1 min read
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

Discussion

pic
Editor guide