typeof let/const/class in TDZ gets an error❗️

using typeof on let/ const/ class in its temporal dead zone will throw an error.

replit:typeof in TDZ

'use strict';    // ⭐ toggle sloppy/strict mode

const { log } = console;

// ┌───────────────────────┐
// │  temporal dead zone   │
// └───────────────────────┘

// ❗ lexical declarations (let/const/class) are still "uninitialized"
typeof aLet;       // ⛔ ReferenceError
typeof aConst;     // ⛔ ReferenceError
typeof aClass;     // ⛔ ReferenceError
// ⛔ ReferenceError: Cannot access '...' before initialization

// ┌───────────────────────┐
// │ undeclared identifier │
// └───────────────────────┘

// ⭐ can't reference an "undeclared identifier" direcctly.
undeclared;        // ⛔ ReferenceError
// ⛔ ReferenceError: 'undeclared' is not defined

// ⭐ but `typeof` can be used with "undeclared identifier"
typeof undeclared;                        // ❗ 'undefined'

// ❗ "var" has no "temporal dead zone", but it's `undefined` initially.
log(aVar === undefined, typeof aVar);     // ❗ true, 'undefined'

// ┌────────────────────────────────────────┐
// │ lexical declarations (let/const/class) │
// └────────────────────────────────────────┘

let aLet;                                 // ⭐ initialized to `undefined`
const aConst = "hello";
class aClass {}

// ┌─────┐
// │ var │
// └─────┘

// ❗ "var" is a "statement", not "declaration".
var aVar;

Last updated