Hoisting in JavaScript
Hoisting moves declarations (not initializations) to the top of their scope before execution: var declarations and function declarations are usable-above-their-line (vars as undefined), while let/const live in the temporal dead zone until their line runs. This isn't physical code motion — the engine simply creates bindings during a setup pass.
Practical fallout: call functions before defining them freely (function declarations hoist fully), never rely on pre-line var values (undefined, not errors), and expect ReferenceErrors — not undefined — from early let/const access.
Declarations lift, values don't
var x; lifts so x reads undefined above; x = 5 stays where written — setup pass creates bindings, execution assigns.
Functions hoist whole
function f() {} is defined before any line runs — calling f() at the file top works.
let/const wait in the TDZ
Their bindings exist but throw on access until the declaration line executes — safer than var's silent undefined.
Write as if none existed
Declare before use anyway — code readable without hoisting knowledge beats code exploiting it.
console.log(greet("Ada"));function greet(name) { return "hi " + name;}console.log(typeof later);var later = 1;Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.