Sobes.tech
Junior — Middle

What does the term TDZ mean and how does it affect code execution in JavaScript?

sobes.tech AI

Answer from AI

TDZ (Temporal Dead Zone) is the period of time between the declaration of a variable using let or const and its actual assignment, during which access to the variable results in an error.

Unlike var, variables declared with let and const are not hoisted with initialization and are in the TDZ until the declaration. If you try to access a variable in the TDZ, a ReferenceError will be thrown.

Example:

console.log(a); // ReferenceError: Cannot access 'a' before initialization
let a = 10;

TDZ helps prevent errors related to using variables before their declaration, making the code more predictable and safe.