LET, CONST and TEMPORAL DEAD ZONE (TDZ);

shahidbugti

Shahid Bugti

Posted on December 23, 2023

LET, CONST and TEMPORAL DEAD ZONE (TDZ);

In JavaScript hoisting of variables declared with “var” is Popular and well known among JavaScript developers.

ES6 Came with two new keywords for variable declaration those are “let” and “const”, unlike “var” declaration the variables declared with “let” *and *“const” are not accessible before declaration,

console.log(name);
var name = "Shahid"; //undefined


console.log(name);
let name = "Shahid"; //Uncaught ReferenceError

Enter fullscreen mode Exit fullscreen mode

Due to this many of developers were lead to the concept that “let” and “const” declaration are not hoisted.

IS THIS CONCEPT TRUE ?
To Understand the answer first you have to understand what Hoisting is?

HOISTING:
The allocation of memory to variable before exicuting a single line of code is called hoisting.

Image description

As you can se the variables declared with “var” and “ let” both are allocated memory and one which is declared with ** “var”** is stored in Global space while the one declared with “let” is stored in a seprate space.

Now You have got the answer that the concept (** “let”** and “const” declaration are not hoisted.”) is Incorrect.

TEMPORAL DEAD ZONE (TDZ):
If “let” and *“const” * declaration are hoisted than why they are not accessible before their declaration? the answer of this depends on concept of TDZ.

“ Time period between Hoisting of a variable and Initializing it with value is Known as Temporal Dead Zone , TDZ”.

console.log(name);
//Temporal dead zone
//Temporal dead zone
//Temporal dead zone
//Temporal dead zone
//Temporal dead zone
//Temporal dead zone
//Temporal dead zone
//Temporal dead zone
let name = "Shahid";
Enter fullscreen mode Exit fullscreen mode

Thanks For taking the time to explore this Article

originally published on medium
https://medium.com/@bugtimuhammadshahid1/let-const-and-temporal-dead-zone-tdz-f33225aee853

Lets Connect on LinkedIn

www.linkedin.com/in/muhammad-shahid-bugti-2a7b43276

💖 💪 🙅 🚩
shahidbugti
Shahid Bugti

Posted on December 23, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related