Skip to content

Latest commit

 

History

History
75 lines (54 loc) · 2.41 KB

File metadata and controls

75 lines (54 loc) · 2.41 KB

let & const in JS, Temporal Dead Zone

let in JavaScript

Introduced in ES6.

  • Block-scoped → only accessible within the {} where they're defined.
  • Hoisted but not initialized → lives in the Temporal Dead Zone until the declaration line.
  • Can be updated but not re-declared in the same scope.

Example:

{
  let a = 10;
  a = 20; // ✅ allowed
  // let a = 30; // ❌ SyntaxError: Identifier 'a' has already been declared
}

const in JavaScript

  • Also block-scoped.
  • Hoisted but not initialized → in TDZ until declaration.
  • Must be initialized at the time of declaration.
  • Cannot be reassigned, but object/array properties can still be mutated.

Example:

{
  const b = 50; // ✅ must initialize
  // b = 60; // ❌ TypeError
  const obj = { x: 1 };
  obj.x = 2; // ✅ allowed
}

Temporal Dead Zone (TDZ)

Definition: The period between entering a scope (block/function) and the actual declaration of a let or const variable — during which the variable exists but cannot be accessed.

Key Points:

  • Variables in TDZ are hoisted, but not given a default value (undefined).
  • Accessing them before declaration → ReferenceError.
  • Prevents accidental usage of variables before initialization.

Example:

console.log(x); // ReferenceError (TDZ)
let x = 5;
console.log(x); // 5

Why TDZ Exists

  • Makes code more predictable.
  • Helps catch errors where variables are used before being properly initialized.

var vs let vs const (Quick Table)

Feature var let const
Scope Function Block Block
Hoisted? Yes Yes Yes
Initialized on hoist? Yes (undefined) No (TDZ) No (TDZ)
Redeclaration allowed? Yes No No
Reassignment allowed? Yes Yes No

Interview Soundbite

"Both let and const are block-scoped and hoisted, but unlike var, they're not initialized until the actual declaration line — the period before that is the Temporal Dead Zone. let can be reassigned but not redeclared, while const must be initialized and can't be reassigned, though its object properties can be changed."