What is TDZ in Javacript engine?

Amish Shabani
1 min readSep 2, 2023

--

TDZ stands for “Temporal Dead Zone” in JavaScript. It refers to the period of time in which a variable exists but cannot be accessed or referenced before it is declared with the let or const keywords.

In JavaScript, variables declared with let and const are hoisted to the top of their respective scopes, but they are not initialized immediately. Instead, they enter the TDZ until their declarations are reached during the execution of the code.

During the TDZ, if you try to access or reference a variable, a ReferenceError will be thrown. This is because the variable exists in the scope but has not been assigned any value yet.

For example:

In this example, trying to access myVariable before its declaration will result in a ReferenceError due to the TDZ. Once the variable is declared, it can be accessed normally.

The TDZ is a mechanism in JavaScript that helps catch potential errors caused by accessing variables before they are declared and assigned a value. It promotes good coding practices and prevents accidental variable usage before initialization.

--

--

No responses yet