Last updated 2 years ago
JS ⟩ value ⟩ type ⟩ name ⟩ typeof ⟩ no typeof let/const/class in TDZ
JS ⟩ operator ⟩ unary ⟩ typeof ⟩ no typeof let/const/class in TDZ
using typeof on let/ const/ class in its temporal dead zone will throw an error.
ReferenceError:cannot access '...' before initialization
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;
typeof null === "object" // true
typeof
typeName() - refined version of typeof.
lexical declaration - let / const / class.
cannot access 'xxx' before initialization❗️