⛔cannot access 'xxx' before initialization❗️
// ⭐ 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 {}const { log } = console;
function sayHi() {
var greeting = "Hello";
// block scope
{
// --------------------------
// ⭐️ cause runtime error❗
// --------------------------
greeting = "Howdy"; // ╮
// ^^^^^^^^ // ⭐️
// ⛔ ReferenceError: (runtime error) // Temporal
// Cannot access 'greeting' // Dead
// before initialization // Zone (TDZ)
// for `greeting`
// ╯
let greeting = "Hi"; // ⭐️ `greeting` declared here❗
log(greeting);
}
}
log('hello'); // ✅ 'hello' (this line is OK)
sayHi(); // ⛔ run-time errorLast updated