{
    "slug": "js_range_errors",
    "term": "RangeError — Stack Overflow & Invalid Values",
    "category": "javascript",
    "difficulty": "intermediate",
    "short": "RangeError is thrown when a value is outside its allowed range — most commonly from infinite recursion (stack overflow) or invalid array/string sizes.",
    "long": "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.",
    "aliases": [],
    "tags": [
        "javascript",
        "errors",
        "recursion",
        "range-error"
    ],
    "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."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "js_reference_errors",
        "js_syntax_errors",
        "php_stack_overflow",
        "js_event_loop_blocking"
    ],
    "prerequisites": [
        "js_reference_errors"
    ],
    "refs": [
        "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError"
    ],
    "bad_code": "function flatten(arr) {\n    return arr.reduce((acc, val) =>\n        Array.isArray(val) ? acc.concat(flatten(val)) : acc.concat(val), []\n    );\n}\nflatten(deeplyNestedArray); // RangeError: Maximum call stack size exceeded",
    "good_code": "// Iterative with explicit stack:\nfunction flatten(arr) {\n    const stack = [...arr];\n    const result = [];\n    while (stack.length) {\n        const item = stack.pop();\n        if (Array.isArray(item)) stack.push(...item);\n        else result.push(item);\n    }\n    return result.reverse();\n}\n\n// Or with depth limit:\nfunction flatten(arr, depth = 10) {\n    if (depth === 0) return arr;\n    return arr.flat(depth);\n}",
    "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.",
    "severity": "high",
    "effort": "medium",
    "updated": "2026-04-05",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/js_range_errors",
        "html_url": "https://codeclaritylab.com/glossary/js_range_errors",
        "json_url": "https://codeclaritylab.com/glossary/js_range_errors.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[RangeError — Stack Overflow & Invalid Values](https://codeclaritylab.com/glossary/js_range_errors) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/js_range_errors"
            }
        }
    }
}