{
  "id": "temporal-dead-zone",
  "title": "Temporal dead zone",
  "lang": "en",
  "source_lang": "en",
  "summary": "A temporal dead zone is the interval during JavaScript execution in which a lexical binding exists but cannot be accessed because it has not yet been initialized.",
  "category": "computer-science",
  "subcategory": "software",
  "type": "concept",
  "aliases": [
    "TDZ"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/temporal-dead-zone/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/temporal-dead-zone/",
      "json": "https://aiwiki.page/zh/temporal-dead-zone.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "A temporal dead zone (TDZ) is the interval during JavaScript execution in which a binding has been created but has not yet been initialized. Reading or assigning to that binding during this interval throws a ReferenceError, rather than returning or storing a value. The term is most commonly associated with variables declared using let and const, but uninitialized bindings also arise with class declarations, function parameters, and module imports. The distinction is between a binding that exists without an initialized value and one whose initialized value happens to be undefined."
    },
    {
      "heading": "Binding creation and initialization",
      "text": "JavaScript distinguishes several operations that are often loosely described as “declaring a variable”:\n\n- Binding creation establishes an association between an identifier and a storage location in an environment.\n- Initialization gives the binding its first value and makes it accessible.\n- Assignment changes the value of an already initialized, mutable binding.\n\nFor lexical declarations, binding creation generally happens when the relevant environment is established, before the statements in that environment execute. Initialization happens later, when execution reaches the appropriate step of the declaration.\n\n{\n  console.log(count); // ReferenceError\n  let count = 3;\n}\n\nThe binding for count exists when the block begins, but it remains uninitialized until the declaration initializes it to 3. Because the exception interrupts execution, that initialization is not reached in this example.\n\nA declaration without an initializer also ends the TDZ when it executes:\n\n{\n  let count;\n  console.log(count); // undefined\n}\n\nHere, executing let count; initializes the binding to undefined. After that point, reading it is permitted.\n\nAn initializer is evaluated before its binding is initialized:\n\nlet count = count + 1; // ReferenceError\n\nThe occurrence of count on the right-hand side refers to the binding being declared, which is still in its TDZ.\n\nOrdinary const declarations require an initializer. Their bindings are immutable after initialization, but immutability and the TDZ are separate rules: accessing an uninitialized const binding throws a ReferenceError, whereas attempting to reassign it after initialization throws a TypeError."
    },
    {
      "heading": "Scope, shadowing, and hoisting",
      "text": "The TDZ is closely related to lexical scope. A lexical binding governs name resolution throughout its scope, including the portion before its declaration executes. An uninitialized inner binding therefore hides an outer binding with the same name:\n\nconst label = \"outer\";\n\n{\n  console.log(label); // ReferenceError, not \"outer\"\n  const label = \"inner\";\n}\n\nJavaScript does not fall back to the outer label merely because the inner one is uninitialized.\n\nThis behavior is frequently discussed in terms of hoisting, an informal description of the effects of declaration processing before statement execution. Saying that let and const “are not hoisted” can be misleading: their bindings affect name resolution before their declarations execute, but they are not initialized at the beginning of the scope.\n\nBy contrast, a var binding is normally initialized to undefined when its variable environment is instantiated:\n\nconsole.log(value); // undefined\nvar value = 10;\n\nThe later initializer assigns 10; it does not mark the first point at which the binding becomes readable.\n\nThe TDZ model was introduced into standardized JavaScript with the lexical declarations of ECMAScript 2015. It provides block-scoped bindings without making premature access silently produce undefined."
    },
    {
      "heading": "Why the dead zone is temporal",
      "text": "The boundary depends on execution order, not simply on an identifier’s position in the source code. A function may contain a reference written before a declaration and still use that binding successfully if it runs after initialization:\n\n{\n  const readValue = () => value;\n  let value = 42;\n\n  console.log(readValue()); // 42\n}\n\nThe function forms a closure over the surrounding environment. Creating it does not read value; calling it does.\n\nCalling the same function too early produces an error:\n\n{\n  const readValue = () => value;\n\n  readValue(); // ReferenceError\n  let value = 42;\n}\n\nThe outcome therefore depends on the program’s control flow. The same textual reference can be valid or invalid depending on when it is evaluated."
    },
    {
      "heading": "The special case of typeof",
      "text": "The typeof operator normally allows an unresolvable identifier to be inspected without throwing:\n\ntypeof missingIdentifier; // \"undefined\", if no such binding exists\n\nIt does not provide that protection for an existing but uninitialized lexical binding:\n\n{\n  typeof item; // ReferenceError\n  let item = 1;\n}\n\nThese cases differ semantically. In the first, name resolution finds no binding. In the second, it finds a binding whose value cannot yet be accessed. Consequently, typeof name === \"undefined\" is not a universally safe test when name might refer to a binding in its TDZ."
    },
    {
      "heading": "Other contexts",
      "text": "Class declarations\n\nAn outer binding introduced by a class declaration remains uninitialized until class evaluation reaches the step that initializes it:\n\nnew Widget(); // ReferenceError\n\nclass Widget {}\n\nThis differs from ordinary function declarations, whose bindings are generally initialized before execution of the surrounding statements. Class evaluation also involves an internal class-name binding, so its initialization rules are more detailed than simply making a class available at the beginning of its scope.\n\nDefault parameter initializers\n\nFunction parameters are initialized in order. An earlier parameter’s default initializer can read an already initialized earlier parameter, but it cannot read a later parameter that is still uninitialized:\n\nfunction valid(first = 1, second = first) {\n  return second;\n}\n\nvalid(); // 1\n\nfunction invalid(first = second, second = 1) {\n  return first;\n}\n\ninvalid(); // ReferenceError\n\nThe error in invalid() occurs while evaluating its default arguments, before its function body begins.\n\nLoop declarations\n\nLoop headers can expose less obvious TDZ behavior:\n\nconst values = [1, 2, 3];\n\nfor (let values of values) {\n  // Not reached\n}\n\nWhile the iterable expression on the right of of is evaluated, the loop’s inner values binding shadows the outer one but remains uninitialized. The expression therefore throws a ReferenceError.\n\nModule imports\n\nJavaScript module imports are live bindings to exports rather than independent copies of exported values. In a cyclic module dependency, one module may attempt to read an exported lexical binding before the exporting module has initialized it. That access throws a ReferenceError.\n\nA circular dependency does not necessarily cause such an error. The outcome depends on module evaluation order, the kind of exported declaration, and when the imported binding is read."
    },
    {
      "heading": "Language semantics and implementation",
      "text": "The TDZ is a language-level rule, not a description of unused or inaccessible physical memory. An implementation may represent an uninitialized binding with an internal sentinel and check for it during access. A compiler can eliminate a check when it proves that initialization must already have occurred, provided observable behavior remains unchanged.\n\nThe rule concerns bindings, not arbitrary object properties or missing values. Reading a nonexistent object property commonly yields undefined; reading a lexical binding in its TDZ throws. Likewise, a TDZ failure is ordinarily a runtime exception, distinct from a syntax error such as declaring an ordinary const without an initializer."
    }
  ],
  "links": [
    "javascript",
    "source-code",
    "control-flow",
    "compiler"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}