TryBugHunt

TypeError: Reduce of empty array with no initial value

TypeError: Reduce of empty array with no initial value in JavaScript means you called arr.reduce() on an array that contains zero items, without passing a second argument as the initial accumulator value.

JavaScriptPattern: Null and undefined errors

How do I fix TypeError: Reduce of empty array with no initial value?

To fix TypeError: Reduce of empty array with no initial value in JavaScript, locate the line causing the error and check the values right before it runs. Most occurrences happen because of filtering an array down to zero items before reduce. Apply a defensive check or safe fallback, make sure collections or objects exist before access, and verify return values from upstream functions.

1. Inspect bounds & typesPrint or inspect collection sizes and verify variables are not empty or None/undefined.
2. Guard with safe accessUse optional chaining (?.), dictionary get(), or guard conditions before dereferencing.
3. Trace upstream originsFind where the value was instantiated or returned rather than only patching the symptom.

What does TypeError: Reduce of empty array with no initial value mean?

When no initial value is passed to Array.prototype.reduce(), JavaScript uses the first array element arr[0] as the accumulator and starts iterating from index 1. If the array is empty, there is no first element to use, so JavaScript throws a TypeError.

Why does TypeError: Reduce of empty array with no initial value happen?

1. Filtering an array down to zero items before reduce

If no items match the filter condition, the resulting array is [] and reduce crashes.

Breaks

const total = items
  .filter(i => i.inStock)
  .reduce((sum, i) => sum + i.price);

Works

const total = items
  .filter(i => i.inStock)
  .reduce((sum, i) => sum + i.price, 0);

2. Calculating a sum on user cart or transaction list

New users start with an empty cart or history, so items.reduce() throws immediately.

Breaks

const total = cart.reduce((acc, item) => acc + item.total);

Works

const total = cart.reduce((acc, item) => acc + item.total, 0);

3. Finding the maximum item in an empty array

reduce((max, x) => x > max ? x : max) without a starting boundary value.

Breaks

const max = scores.reduce((m, s) => Math.max(m, s));

Works

const max = scores.length ? scores.reduce((m, s) => Math.max(m, s)) : 0;

How to find it in your own code

Always supply a second argument (such as 0 for totals, "" for strings, or {} for mappings) to array.reduce(fn, initialValue).

Try it: fix this bug

easy

getUserCity({}) throws TypeError: Cannot read properties of undefined (reading 'city').

function getUserCity(user) {
    return user.address.city;
}
Open with hintsRuns in your browser. No account needed.

More challenges with this error

Each of these challenges runs into this error or pattern. Find the bug and fix it in your browser โ€” no install, no account needed.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.