reduce in Practice
reduce folds an array into a single value with an accumulator. Always give an initial value. Use it for sums, counts, grouping and building objects.
Example
const nums = [1, 2, 3, 4];
console.log(nums.reduce((s, n) => s + n, 0));
const words = ["a", "b", "a"];
console.log(words.reduce((acc, w) => ({ ...acc, [w]: (acc[w] || 0) + 1 }), {}));Exercise
Starting value for a sum.
arr.reduce((s, n) => s + n, )
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.