What is hoisting in JavaScript?

Beginner

Answer

Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their scope during compilation. Variables declared with var are hoisted and initialized with undefined, while let and const are hoisted but not initialized (temporal dead zone).

console.log(x); // undefined (not error)
var x = 5;
console.log(y); // ReferenceError
let y = 10;