Hoisting and Execution Context
How does the temporal dead zone affect accessing let variables?
console.log(x);
let x = 10;
This code will throw a `ReferenceError: Cannot access 'x' before initialization`. Unlike variables declared with `var`, variables declared with `let` and `const` are hoisted but they are not initialized with a default value. Instead, they remain in a 'temporal dead zone' (TDZ) from the start of the block until the declaration is processed. During this TDZ, any attempt to access the variable will result in a ReferenceError. This behavior was introduced in ES6 (ES2015) with `let` and `const` to help catch potential bugs caused by accessing variables before they're declared. It enforces better coding practices by making the error explicit rather than returning the often-confusing `undefined` value that occurs with `var` declarations. This stricter behavior helps developers create more predictable code and avoid subtle bugs related to variable initialization.