Interview Question

What is reduce()?

reduce folds array elements into one accumulated value.

💡 Concept ✅ Quick Revision ⚡ JavaScript

Answer

reduce combines array elements into one accumulated result. • The callback receives the accumulator and current element. • An explicit initial value avoids special empty-array behavior. • The accumulator can be any JavaScript value.

Example

Code
const total = [1, 2, 3].reduce((sum, value) => sum + value, 0);
console.log(total);
Output
6

Quick Revision

reduce folds array elements into one accumulated value.