How to sum array elements using reduce() method in JavaScript

2 Answers

0 votes
const array = [1, 2, 3, 4, 5, 6];

const sum = array.reduce(function(accumulator, currentValue) {
        return accumulator + currentValue;
}, 0);


console.log(sum);




/*
run:

21

*/

 



answered Oct 17, 2022 by avibootz
0 votes
const array = [1, 2, 3, 4, 5, 6];

const sum = array.reduce((curr, next) => curr + next);

console.log(sum);




/*
run:

21

*/

 



answered Oct 17, 2022 by avibootz
...