⛔temporal dead zone
JS ⟩ variable ⟩ temporal dead zone
the term "temporal" is used because the zone depends on the order of execution (time) rather than the order of place in which the code is written (position).
{
// TDZ (for `a`) starts
const f = () => console.log(a); // - OK: `a` hasn't been actually referenced.
let a = 3; // TDZ (for `a`) ends.
f(); // OK: `a` called outside TDZ.
}replit:"temporal" in time
using typeof on lexical declaration (let/ const/ class) in its temporal dead zone will throw a ReferenceError.
// ⭐ temporal dead zone (start)
// ------------------------------
// typeof aLet; // ⛔ ReferenceError: Cannot access 'aLet' before initialization
// typeof aConst; // ⛔ ReferenceError: Cannot access 'aConst' before initialization
// typeof aClass; // ⛔ ReferenceError: Cannot access 'aClass' before initialization
typeof undeclared; // ❗ 'undefined'
// ⭐ lexical declarations (let/const/class)
// --------------------------------------------
let aLet; // ⭐ initialized to `undefined`
const aConst = "hello";
class aClass {}replit:TDZ: let/const/class
replit:hoisting & TDZ
replit:TDZ: let/const/class
lexical declaration - let/const/class.
Last updated
Was this helpful?