๐ŸŽˆ JavaScript

JavaScript Hoisting โ€” Why Your Code Runs Before It Exists

๐Ÿ“… Jun 25, 2026 โฑ 3 min read

Before running your code, JavaScript scans it and registers declarations first. That is hoisting โ€” and it explains several "impossible" behaviours.

The three cases

sayHi();                    // โœ… works! function declarations hoist FULLY
function sayHi() { console.log("hi"); }

console.log(a);             // undefined โ€” var hoists, value doesn't
var a = 5;

console.log(b);             // โŒ ReferenceError โ€” TDZ
let b = 5;

The temporal dead zone (TDZ)

let/const ARE hoisted โ€” but locked until their declaration line. Accessing them early throws instead of silently giving undefined. This is a feature: it catches bugs var would hide.

Function expressions do not hoist

greet();                    // โŒ TypeError: greet is not a function
var greet = function() {};  // only `var greet` hoisted (as undefined)

Practical rule: declare before use, use const/let, and hoisting becomes trivia you only need for interviews โ€” where it absolutely will appear.

โ† All Articles