Hoisting and Temporal Dead Zone
Hoisting is JavaScript’s behavior of moving declarations to the top of their scope before code execution.
vardeclarations are hoisted and initialized withundefined.letandconstare hoisted but not initialized – they stay in a “temporal dead zone” (TDZ) until the declaration is evaluated.
Example with var:
console.log(x); // undefined (not an error)var x = 5;Example with let (TDZ):
console.log(y); // ReferenceError: Cannot access 'y' before initializationlet y = 10;The TDZ exists from the start of the block until the declaration is encountered. Accessing the variable in that zone throws an error.
Function declarations are fully hoisted (both declaration and body). You can call them before they appear.
sayHi(); // "Hello!"function sayHi() { console.log("Hello!");}Best practice: Declare variables at the top of their scope to avoid TDZ confusion.