← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

RangeError — Stack Overflow & Invalid Values

JavaScript ES5 Intermediate
debt(d5/e3/b3/t7)
d5 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'specialist tool catches it' (d5), ESLint can detect some recursive patterns and missing base cases via linting rules, but stack overflow from RangeError itself often manifests only at runtime with specific input depths; catching the actual crash requires runtime testing or profiling, not static analysis.

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3), the quick_fix of adding depth counters is a localised change within a function or small refactor to replace recursion with iteration in one component; no cross-file coordination needed.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3), the choice to use or avoid deep recursion affects individual functions and their callers, but doesn't reshape the broader architecture; however, if recursion is endemic to a codebase, retrofitting depth limits becomes a persistent tax.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7), the misconception that try/catch always catches stack overflow RangeError contradicts normal exception-handling expectations; developers expect catch blocks to work, but engine crashes before the catch can run, violating the principle of exception safety that applies to other error types in the same language.

About DEBT scoring →

TL;DR

RangeError is thrown when a value is outside its allowed range — most commonly from infinite recursion (stack overflow) or invalid array/string sizes.

Explanation

RangeError: Maximum call stack size exceeded is the most common form — infinite or too-deep recursion. Other causes: new Array(-1) (negative size), toFixed(200) (precision out of range), String.fromCodePoint(Infinity). Unlike ReferenceError (undeclared variable) or TypeError (wrong type), RangeError is about a value being outside its valid numeric or structural range. Catching: try/catch catches RangeError from bad API calls but NOT stack overflow from recursion in most engines — stack overflow crashes the current execution context. Detection: check for recursion with a depth counter, use iterative algorithms for large inputs.

Common Misconception

try/catch always catches stack overflow RangeError — stack overflow from deep recursion often crashes the JS engine context before catch can run. Guard with depth counters instead.

Why It Matters

RangeError from stack overflow is the most common crash in recursive algorithms on user-supplied data — depth-limiting is essential for production code.

Common Mistakes

  • Recursive functions without a base case or depth limit.
  • Calling toFixed() with user-supplied precision — can be > 100 (throws RangeError).
  • Not knowing new Array(n) throws for negative n.

Code Examples

✗ Vulnerable
function flatten(arr) {
    return arr.reduce((acc, val) =>
        Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val), []
    );
}
flatten(deeplyNestedArray); // RangeError: Maximum call stack size exceeded
✓ Fixed
// Iterative with explicit stack:
function flatten(arr) {
    const stack = [...arr];
    const result = [];
    while (stack.length) {
        const item = stack.pop();
        if (Array.isArray(item)) stack.push(...item);
        else result.push(item);
    }
    return result.reverse();
}

// Or with depth limit:
function flatten(arr, depth = 10) {
    if (depth === 0) return arr;
    return arr.flat(depth);
}

Edited 5 Apr 2026
Views 69
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 1 ping F 0 pings S 0 pings S 2 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W
No pings yet today
No pings yesterday
Amazonbot 7 Perplexity 6 ChatGPT 3 Unknown AI 3 Meta AI 3 Ahrefs 3 Majestic 2 Google 2 Claude 2 Scrapy 1 Sogou 1
crawler 29 crawler_json 3 pre-tracking 1
DEV INTEL Tools & Severity
🟠 High ⚙ Fix effort: Medium
⚡ Quick Fix
Add depth counters to recursive functions. Use iterative algorithms with explicit stacks for large inputs. Validate numeric args before passing to toFixed/Array constructor.
📦 Applies To
javascript ES5 web cli
🔗 Prerequisites
🔍 Detection Hints
function.*\(.*\).*{[^}]*\(
Auto-detectable: ✗ No eslint
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: Medium ✗ Manual fix Fix: High Context: Function Tests: Update
CWE-674 CWE-400


✓ schema.org compliant