JavaScript Hoisting Explained
Understand how JavaScript hoists variable and function declarations.

What is Hoisting?#
Hoisting is JavaScript's behavior of moving declarations to the top of their scope before code execution.
Variable Hoisting#
Code
// var is hoisted and initialized with undefined
console.log(x); // undefined
var x = 5;
// let/const are hoisted but not initialized (TDZ)
console.log(y); // ReferenceError
let y = 5;Function Hoisting#
Code
// Function declarations are hoisted
sayHello(); // "Hello"
function sayHello() {
console.log('Hello');
}
// Function expressions are NOT hoisted
sayHi(); // TypeError
const sayHi = () => {
console.log('Hi');
};Temporal Dead Zone#
The TDZ is the time between entering scope and variable initialization where the variable cannot be accessed.