18
Temporal Dead Zone
2021-02-17 - JavaScript
1// JS Tip of the Day
2// Temporal Dead Zone
3
4let hero = 'Superman';
5
6// ...
7
8if (hero) { // Enter in a new scope
9 // 💀 We are inside the Temporal Dead Zone for hero 💀
10
11 /*
12 let declaration is block scoped and it isn't afflicted by Hoisting but live under the TDZ!
13 We cannot use it before its block declaration or we trigger a ReferenceError
14 */
15 console.log(hero); // ReferenceError
16
17 // 💀 We are inside the Temporal Dead Zone for hero 💀
18
19 let hero; // TDZ is ending and hero è undefined
20 /*
21 hero is also declared inside this scope with let and it create a block scope!
22 */
23
24 hero = 'Batman';
25 console.log('Batman'); // Batman
26}
27
28console.log(hero); // Superman;
#JStipoftheday