What is the Array reduce() method?

Answer

The reduce() method reduces an array to a single value by executing a callback for each element, accumulating the result. Syntax: array.reduce((accumulator, currentValue, index, array) => newAccumulator, initialValue). Sum: [1,2,3,4].reduce((sum, n) => sum + n, 0) → 10. Count occurrences: words.reduce((counts, word) => ({...counts, [word]: (counts[word] || 0) + 1}), {}). Flatten: arr.reduce((flat, sub) => flat.concat(sub), []). Always provide the initial value as the second argument — without it, the first element is used as the initial accumulator and the callback starts from index 1, which causes bugs on empty arrays. Reduce is the most powerful array method — map and filter can both be implemented using reduce, though using the specialized methods is clearer.