JavaScript array.reduce() Function

JavaScript Logo

array.reduce() – Reduces the array to a **single value** by executing the callback on each element and accumulating the result.

๐Ÿ‘‰ Syntax

result = array.reduce((accumulator, current, index, array) => {
  return /* next accumulator */;
}, initialValue);

๐Ÿงช Example

const numbers = [1,2,3,4];
const sum = numbers.reduce((acc, n) => acc + n, 0);
console.log(sum); // 10

Comments