ForgeFrontend — Prepare, Practice, Crack
Secure checkout
Lifetime access
Instant Drive delivery
Free updates forever

Prepare · Practice · Crack

How does reduce work and when would you use it?

Quick answer

reduce folds an array into a single value using an accumulator — sums, objects, grouping, anything.

In detail

`reduce((acc, item) => newAcc, initial)` carries an accumulator across the array. Beyond sums it can build objects (counting, grouping), flatten arrays, or implement map/filter. Always pass an initial value to avoid edge cases on empty arrays.

[1,2,3,4].reduce((s, n) => s + n, 0); // 10
["a","b","a"].reduce((acc, k) => {
  acc[k] = (acc[k] || 0) + 1; return acc;
}, {}); // { a: 2, b: 1 }

Why interviewers ask this: Tests comfort with the most powerful array method.

This is 1 of 118+ questions in the JavaScript Interview Kit

Get every question with detailed answers, follow-ups and real code — plus coding challenges and a last-minute revision sheet. One-time payment, instant access.

⚡ Get the JavaScript Interview Kit → ₹299

Full kit

JavaScript Interview Kit · ₹299

Get it →