JS Coding Questions Logo
JS Coding Questions
#22💼 Interview💻 Code

What is the Temporal Dead Zone

Advertisement

728x90

The Temporal Dead Zone (TDZ) refers to the period between the start of a block and the point where a variable declared with let or const is initialized. During this time, the variable exists in scope but cannot be accessed, and attempting to do so results in a ReferenceError.

This behavior is part of JavaScript's ES6 (ECMAScript 2015) specification and applies only to variables declared with let and const, not var. Variables declared with var are hoisted and initialized with undefined, so accessing them before the declaration does not throw an error, though it can lead to unexpected results.

#### Example

javascript
1function someMethod() {
2  console.log(counter1); // Output: undefined (due to var hoisting)
3  console.log(counter2); // Throws ReferenceError (TDZ for let)
4
5  var counter1 = 1;
6  let counter2 = 2;
7  }

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 23

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
22of476