{
    "slug": "js_temporal_dead_zone",
    "term": "Temporal Dead Zone (TDZ)",
    "category": "javascript",
    "difficulty": "intermediate",
    "short": "let and const variables exist in a Temporal Dead Zone from the start of their block until their declaration — accessing them before throws ReferenceError.",
    "long": "TDZ is the period between entering a scope and reaching a variable's declaration. Unlike var (hoisted and initialised to undefined), let/const are hoisted but not initialised — accessing them in the TDZ throws ReferenceError: Cannot access 'x' before initialization. This is intentional — it prevents the var hoisting confusion. TDZ affects: let/const in blocks, class declarations (classes are in TDZ unlike function declarations), default parameter values that reference earlier parameters. typeof is the only operation that doesn't throw in the TDZ.",
    "aliases": [],
    "tags": [
        "javascript",
        "tdz",
        "hoisting",
        "let",
        "const"
    ],
    "misconception": "let and const are not hoisted — they ARE hoisted (the engine knows they exist in the scope) but remain uninitialised until the declaration line.",
    "why_it_matters": "TDZ errors are a common gotcha when refactoring var to let/const — code that relied on var hoisting breaks with ReferenceError instead of getting undefined.",
    "common_mistakes": [
        "Using a let variable before its declaration in the same block.",
        "Expecting class expressions to behave like function declarations (they're in TDZ).",
        "Not knowing typeof variable still throws in TDZ for let/const."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "js_hoisting",
        "js_reference_errors",
        "js_closure_loop_bug"
    ],
    "prerequisites": [
        "js_hoisting"
    ],
    "refs": [
        "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#temporal_dead_zone_tdz"
    ],
    "bad_code": "console.log(x); // ReferenceError — TDZ\nlet x = 5;\n\n// Class TDZ:\nconst obj = new MyClass(); // ReferenceError\nclass MyClass {}",
    "good_code": "// Always declare before use:\nlet x = 5;\nconsole.log(x); // 5\n\n// Classes too:\nclass MyClass {}\nconst obj = new MyClass(); // Fine\n\n// Safe check without TypeError:\nif (typeof possiblyUndeclared !== 'undefined') {}",
    "quick_fix": "Always declare let/const at the top of their scope. If converting from var, check all usage is after the declaration. Enable ESLint no-use-before-define rule.",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-22",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/js_temporal_dead_zone",
        "html_url": "https://codeclaritylab.com/glossary/js_temporal_dead_zone",
        "json_url": "https://codeclaritylab.com/glossary/js_temporal_dead_zone.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": "[Temporal Dead Zone (TDZ)](https://codeclaritylab.com/glossary/js_temporal_dead_zone) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/js_temporal_dead_zone"
            }
        }
    }
}