What is Temporal Dead Zone(TDZ)

Preeti samuelPreeti samuel
1 min read

Temporal Dead Zone (TDZ) is a concept in JavaScript that refers to the time period between the declaration of a variable and its assignment, during which the variable is in an undefined state.

In JavaScript, variables declared with the "var" keyword are hoisted to the top of the scope in which they are declared. This means that they are available throughout the entire scope, even before they are declared and assigned a value.

However, variables declared with "let" or "const" is not hoisted, and are only available within the block in which they are declared. Before the variable is assigned a value, it is in a TDZ, and attempting to access the variable during this time will result in a ReferenceError.

Here's an example of TDZ in action:

console.log(x);  // ReferenceError: x is not defined
let x = 5;

In the example above, x is declared with let and is in the TDZ before it is assigned the value of 5. Accessing x before it is assigned a value will result in a ReferenceError.

It's important to be aware of the TDZ when working with variables declared with let or const to avoid unintended reference errors in your code.

0
Subscribe to my newsletter

Read articles from Preeti samuel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Preeti samuel
Preeti samuel

I am Kamilla Preeti Samuel, a Fullstack Developer with a strong command of JavaScript, Node.js, MongoDB, MySQL, CSS, and HTML. Over the years, I have built and worked on a range of applications, gaining valuable hands-on experience in both backend and frontend development. My professional journey includes working as a Junior Software Engineer at Bytestrum, where I focused on software development, and at NUK9 as a UX and UI Designer, contributing to creating user-centered design solutions. I thrive on building efficient, scalable, and user-friendly applications, combining technical expertise with a keen eye for design. I enjoy collaborating with cross-functional teams to create seamless digital experiences, and I am passionate about continuously exploring new tools and frameworks to stay ahead in the fast-evolving tech landscape. I am Kamilla Preeti Samuel, a full-stack developer with a strong command of JavaScript, Node.js, MongoDB, MySQL, CSS, and HTML. Over the years, I have built and worked on various applications, gaining valuable hands-on experience in both backend and frontend development. My professional journey includes working as a Junior Software Engineer at Bytestrum, where I focused on software development, and at NUK9 as a UX and UI Designer, contributing to creating user-centered design solutions. I thrive on building efficient, scalable, and user-friendly applications, combining technical expertise with a keen eye for design. I enjoy collaborating with cross-functional teams to create seamless digital experiences, and I am passionate about continuously exploring new tools and frameworks to stay ahead in the fast-evolving tech landscape.