Hoisting
Hoisting is the engine creating a scope's bindings before any line of that scope runs. var and function declarations are usable early; let and const exist but throw until their line initialises them.
Detailed Explanation
JavaScript does not execute a function from top to bottom in one pass. First it instantiates the scope: it walks the source, finds every declaration, and creates bindings. Then it runs the statements. "Hoisting" is the informal name for that first pass. Nothing is physically moved in your file. The bindings are created first.
What you can do with a hoisted name depends on the keyword.
A function declaration is installed as a complete function object during instantiation. Calling it on the line above the declaration works. Two declarations of the same name in one function scope: the last one in source order wins, and it wins for the whole function, including lines above both.
A var binding is created and set to undefined. Reading it before its assignment returns undefined. Assigning to it before its var line also works; the later var x = 1 then assigns 1. var is function-scoped, so a var inside an if is hoisted to the whole function.
A let or const binding is created but uninitialised. The period from the start of the block to the declaration line is the temporal dead zone. Reading or writing the name in that period is a ReferenceError. typeof x is also a ReferenceError for a let x still in the TDZ, which surprises people who learned that typeof is always safe.
class declarations hoist like let: the name exists, the TDZ applies, then the line initialises the class. import bindings are live views of the exported binding and are created before the module body runs, so you can use an imported name on line one.
People say "don't rely on hoisting" and then put helper function declarations at the bottom of a file. That style is fine: declarations are designed for it. What is not fine is relying on var being undefined, or mixing var with let of the same name in ways the spec calls an early error.
What it costs you: reordering code and changing behaviour. Moving a const above its first use is required. Moving a function declaration is optional. Moving a var can hide a bug that only showed as undefined. Prefer const and let, and treat function declarations as the one hoisted form you use on purpose.
Code Examples
console.log(double(4)); // => 8 (declaration is already installed)
function double(n) {
return n * 2;
}
console.log(count); // => undefined (var exists, value not assigned yet)
var count = 3;
console.log(count); // => 3try {
console.log(total);
} catch (err) {
console.log(err.name); // => ReferenceError (TDZ)
}
const total = 10;
console.log(total); // => 10function mixed() {
// both declarations are hoisted; the second function wins everywhere
return pick();
function pick() {
return "first";
}
function pick() {
return "second";
}
}
console.log(mixed()); // => secondCommon Gotchas
`let`/`const`/`class` in the TDZ throw, including with `typeof`
`typeof x` is not a safe existence check for a binding later in the same block.
Duplicate `function` declarations overwrite
in one function scope the last declaration is the only one, even for calls written above it.
`var` hoists to the function, not the block
`if (false) { var x = 1; }` still creates `x` as `undefined` for the whole function.
Imports are available for the whole module
you can call an imported function on line one. The imported binding stays live; reassignments in the exporting module are visible.