dianping/cat · error · TypeError

reduce of empty array with no initial value

Error message

reduce of empty array with no initial value

What it means

ES5-shim fallback Array.prototype.reduce throws 'reduce of empty array with no initial value' when the array length is 0 and no second (initialValue) argument was supplied. With no elements and no seed there is nothing to return, so ES5 mandates a TypeError. Identical to the native browser error; the shim only fires in engines without native reduce.

Source

Thrown at cat-home/src/main/webapp/assets/js/editor/worker-xquery.js:49108

            if (i in self && fun.call(thisp, self[i], i, object)) {
                return true;
            }
        }
        return false;
    };
}
if (!Array.prototype.reduce) {
    Array.prototype.reduce = function reduce(fun /*, initial*/) {
        var object = toObject(this),
            self = splitString && _toString(this) == "[object String]" ?
                this.split("") :
                object,
            length = self.length >>> 0;
        if (_toString(fun) != "[object Function]") {
            throw new TypeError(fun + " is not a function");
        }
        if (!length && arguments.length == 1) {
            throw new TypeError("reduce of empty array with no initial value");
        }

        var i = 0;
        var result;
        if (arguments.length >= 2) {
            result = arguments[1];
        } else {
            do {
                if (i in self) {
                    result = self[i++];
                    break;
                }
                if (++i >= length) {
                    throw new TypeError("reduce of empty array with no initial value");
                }
            } while (true);
        }

View on GitHub (pinned to e815e74d4c)

Solutions

  1. Always pass an initial value: arr.reduce(fn, 0) or arr.reduce(fn, []).
  2. Guard the empty case up front: if (arr.length) { ... } else { default }.
  3. For arrays of objects, an initial value also fixes the accumulator's type on the first iteration.

Example fix

// before
var max = scores.reduce(function(a, b) { return Math.max(a, b); });

// after
var max = scores.reduce(function(a, b) { return Math.max(a, b); }, -Infinity);
Defensive patterns

Strategy: validation

Validate before calling

if (arr.length === 0 && initialValue === undefined) {
  return defaultValue; // or: throw new Error('provide an initial value for reduce');
}

Try / catch

try { total = arr.reduce(fn); }
catch (e) {
  if (e instanceof TypeError && /no initial value/.test(e.message)) {
    total = 0; // safe default for empty input
  } else { throw e; }
}

Prevention

When it happens

Trigger: [].reduce(fn); — empty array literal, an emptied list after filter, or an uninitialized array reduced before data loads (async race).

Common situations: Reducing a filtered result that happens to be empty; reducing before an AJAX/worker response populates the array; optional data sets that are legitimately empty at runtime.

Related errors


AI-assisted analysis of dianping/cat@e815e74d4c (2026-08-14). Data as JSON: /api/errors/3582c067389d9e17. Report an issue: GitHub.