An Interest In:
Web News this Week
- January 14, 2022
- January 13, 2022
- January 12, 2022
- January 11, 2022
- January 10, 2022
- January 9, 2022
- January 8, 2022
Some of Our Sources
- Just Creative
- Vandelay Design
- Creative Curio
- Fuel Your Creativity
- My Ink Blog
- Wal You
- Specky Boy
Temporal Dead Zone
Temporal Dead Zone(TDZ) is basically refers to the duration between a variable declaration and it's initialization. TDZ occurs when we use const and let for declaring our variables.
Let's look into some of the examples that explains this concept.
Like var, let & const are also hoisted but in a different way.
Hoisting example for var:console.log(a); //undefined no errorvar a = 10;
Here in the above examples explains the concept that no matter your variable after declaration is being initialized or not, it will get a value of undefined during execution if you haven't assigned that variable.
Hoisting example for let variables:console.log(b); let b = 20;
Here in the above example you'll get a reference error, this is because a let variable is being allocated memory in separate space [unlike var variable that is being allocated memory in the global object (i.e; the window object)] which we can't access until we assign some value to that variable. That means let variables can't be accessed until assigned some value to it.
Same goes with the const variables also if you try to access them before initialization it will throw a reference error.
So, the basic idea is let and const variables remains in TDZ until it gets some value assigned to it, once the initialization is done TDZ ends for that specific variable.
Tip: To avoid TDZ always try move all your declaration and initialization part to the top of your program so that before the main logic the execution hits the initialization part.
So, that's it for this time. I hope I made the points clear.
Original Link: https://dev.to/sahiba0915/temporal-dead-zone-1ahe