Skip to content

Hoisting

Medium — good to knowFrontend

ELI5 — The Vibe Check

Hoisting is JavaScript's weird quirk where variable and function declarations are mentally 'moved' to the top of their scope before code runs. So you can call a function before you write it in the file. Variables declared with var are hoisted but initialized as undefined, which causes confusing bugs. This is why everyone uses let and const now.

Real Talk

Hoisting is a JavaScript mechanism where variable and function declarations are moved to the top of their containing scope during the compilation phase, before code execution. Function declarations are fully hoisted (callable before declaration). var declarations are hoisted but initialized as undefined. let and const are hoisted but remain in a 'temporal dead zone' until their declaration line.

Show Me The Code

// Function hoisting — works fine
console.log(greet()) // 'Hello!'
function greet() { return 'Hello!' }

// var hoisting — confusing!
console.log(x) // undefined (not an error!)
var x = 5

// let — temporal dead zone
console.log(y) // ReferenceError
let y = 5

When You'll Hear This

Avoid relying on hoisting — declare variables before using them.,var hoisting is a common source of subtle bugs.,Prefer const/let over var to avoid hoisting confusion.

Made with passive-aggressive love by manoga.digital. Powered by Claude.