Sum Program using reduce() method in JavaScript


Get the sum of array values using reduce()

var numbersArary = [2,5,6];
var newResultValue = numbersArary.reduce(testFunc,0)

function testFunc(accumulator,number) {
  return accumulator + number;
}

console.log(newResultValue);
//Output : 13

Get the sum of array values by reduce() method using arrow function

const numbersArary = [2,5,6];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 2 + 5 + 6
console.log(numbersArary.reduce(reducer));
// Output : 13

// 3 + 2 + 5 + 6
console.log(numbersArary.reduce(reducer, 3));
// expected output: 16


Related

JavaScript Average Program using Arrow Function
JavaScript Product Program using Arrow Function
Sum Program using Rest Paramater in JavaScript