Fundamentals

Hoisting

Why declarations appear to move to the top of their scope.

beginner Freshers

Hoisting is the JavaScript engine moving declarations to the top of their scope during compilation, before any code runs.

The engine reads the guest list (declarations) before the party starts, but the guests (values) only arrive when their line runs.

Key concepts

1
Function declarations are hoisted completely, so you can call them before they appear. var declarations are hoisted but not their assignments — the variable exists as undefined until the assignment line executes.
varundefined
2
let and const are hoisted into the temporal dead zone: they exist but cannot be accessed until declared, which turns silent undefined bugs into loud errors.
temporal dead zoneletconstundefined