{"record":{"id":"3582c067389d9e17","repo":"dianping/cat","slug":"reduce-of-empty-array-with-no-initial-value","errorCode":null,"errorMessage":"reduce of empty array with no initial value","messagePattern":"reduce of empty array with no initial value","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"cat-home/src/main/webapp/assets/js/editor/worker-xquery.js","lineNumber":49108,"sourceCode":"            if (i in self && fun.call(thisp, self[i], i, object)) {\n                return true;\n            }\n        }\n        return false;\n    };\n}\nif (!Array.prototype.reduce) {\n    Array.prototype.reduce = function reduce(fun /*, initial*/) {\n        var object = toObject(this),\n            self = splitString && _toString(this) == \"[object String]\" ?\n                this.split(\"\") :\n                object,\n            length = self.length >>> 0;\n        if (_toString(fun) != \"[object Function]\") {\n            throw new TypeError(fun + \" is not a function\");\n        }\n        if (!length && arguments.length == 1) {\n            throw new TypeError(\"reduce of empty array with no initial value\");\n        }\n\n        var i = 0;\n        var result;\n        if (arguments.length >= 2) {\n            result = arguments[1];\n        } else {\n            do {\n                if (i in self) {\n                    result = self[i++];\n                    break;\n                }\n                if (++i >= length) {\n                    throw new TypeError(\"reduce of empty array with no initial value\");\n                }\n            } while (true);\n        }\n","sourceCodeStart":49090,"sourceCodeEnd":49126,"githubUrl":"https://github.com/dianping/cat/blob/e815e74d4c2dd74edac831241f1253fcc7d25381/cat-home/src/main/webapp/assets/js/editor/worker-xquery.js#L49090-L49126","documentation":"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.","triggerScenarios":"[].reduce(fn); — empty array literal, an emptied list after filter, or an uninitialized array reduced before data loads (async race).","commonSituations":"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.","solutions":["Always pass an initial value: arr.reduce(fn, 0) or arr.reduce(fn, []).","Guard the empty case up front: if (arr.length) { ... } else { default }.","For arrays of objects, an initial value also fixes the accumulator's type on the first iteration."],"exampleFix":"// before\nvar max = scores.reduce(function(a, b) { return Math.max(a, b); });\n\n// after\nvar max = scores.reduce(function(a, b) { return Math.max(a, b); }, -Infinity);","handlingStrategy":"validation","validationCode":"if (arr.length === 0 && initialValue === undefined) {\n  return defaultValue; // or: throw new Error('provide an initial value for reduce');\n}","typeGuard":null,"tryCatchPattern":"try { total = arr.reduce(fn); }\ncatch (e) {\n  if (e instanceof TypeError && /no initial value/.test(e.message)) {\n    total = 0; // safe default for empty input\n  } else { throw e; }\n}","preventionTips":["Make it a habit to always pass an initial value to reduce.","For async-populated arrays, reduce only after data arrives (guard on loaded state)."],"tags":["javascript","array","es5-shim","typeerror","empty-array"],"backgroundTag":null,"analyzedSha":"e815e74d4c2dd74edac831241f1253fcc7d25381","analyzedAt":"2026-08-14T14:22:34.512Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}