![]() |
VOOZH | about |
Introduction: Before going to start, just think about what these 3 words are saying? Temporal means something temporary, not permanent, and Dead means something which is not working or can say it is in a lifeless state, Zone denotes an area but here we are in programming so this term area will be something related to memory, or also zone can be thought of as the time period or phase. So merging these terms narrates that some entity is temporarily in a lifeless or inactive state and cannot be used for any kind of work. You should have knowledge of let and const keywords in javascript to understand the topic easily.
Why temporal dead zone exists?This question may arise that they are not in earlier javascript then why we need them, And why ECMAscript introduced this additional thing in new updates, actually in programs accessing the variable before assigning a value is bad, Ok let's say javascript has given some space to variable in memory just because there is a specific reason that you may be going to store some data there, then why to extract that data before actually putting. Most of the programming languages have different configurations for this scenario, like C++ stores garbage before initialization, python variable even doesn't get created before initialization, and java stores a default value.
So javascript has undefined but because of the memory allocation phase hoisting of variables takes place and during the thread execution phase we can access them, consequently, the value appears as undefined. Later when the let & const were introduced they have some restrictions for accessing just to help javascript developers write good code and make debugging easy and so the new concept of temporal dead zone came along, it helps us to catch errors because accessing some data before providing is wrong. This is a language construct that saves us from a lot of unexpected errors.
Now let's understand with different examples.
Example 1: In this example, we are simply using a javascript code snippet and using the var keyword to see how the memory allocation takes place.
Output:
👁 ImageExplanation:
Example 2: Now let's test out the same code snippet by using the let or const keyword.
Output:
👁 ImageExplanation:
Scope of Variables: Now let's see the scope where these variables are stored, notice the var x is undefined and stored inside the global object and the let z is also undefined but stored at another place named Script, It means they both are not the same and the javascript engine wants to differ something during their access. The let and const variables are not accessible before they are initialized with some value, and the phase between the starting of the execution of block in which the let or const variable is declared till that variable is being initialized is called Temporal Dead Zone for the variable. And during this zone javascript will always through a reference error if anyone tries to access those variables.
👁 ImageExample 3: In this example, we will see the variables which are in temporal dead zone.
Output:
👁 ImageExplanation:
Way to avoid the most common reference/type errors and other errors due to temporal dead zone:
The most efficient way to overcome the errors of temporal dead zone is to initialize the variables at the top of the scope so that when our code starts running it completes the initializing part at first and then use those variables otherwise you can run into a lot of unexpected errors in JavaScript code.
Let's see an example-
Output: