What Is Hoisting in JavaScript and What Really Gets Hoisted
Hoisting means JavaScript registers declarations before running code, so function declarations work early and var reads undefined. See what each kind does.

Call a function before the line that defines it and it works. Read a variable before the line that assigns it and you get undefined. Do the same with a different keyword and you get an error. All three behaviors come from one mechanism, which has a confusing name and a simple explanation: before a scope's code runs, the engine reads through it and registers every declaration, each in an initial state that depends on its kind. This post describes what the engine does, what state each declaration starts in, and the situations where the shorthand "declarations move to the top" misleads. Examples run on Node.js 24 as ES modules. The sibling topics are the temporal dead zone and var, let and const, and the whole subject sits in the guide to scope, hoisting and closures.
What the engine does before running
When a function, module or block begins to execute, the engine first creates its scope and walks the code looking for declarations. Each name found is created in the scope. Only then does the first line run. That walk is what "hoisting" names. Nothing is moved: the source text stays where you wrote it, and the engine already knows every name by the time it executes line one.
What differs is the initial state of each kind of binding.
| Declaration | State at the start of the scope |
|---|---|
function name() {} | Initialized with the full function |
var name | Initialized to undefined |
let name, const name | Created, uninitialized (the temporal dead zone) |
class Name {} | Created, uninitialized |
import | Initialized, bound to the other module's export |
That table is the whole topic. The rest is consequences.
Function declarations work early
A function declaration is initialized with its body at the start, so it can be called from anywhere in its scope, including above the definition.
console.log(total(2, 3));
function total(a, b) {
return a + b;
}
console.log(typeof helper);
function helper() {}5
functionThe call at the top works because total was already a function when the first line ran. This is why many codebases put the main flow at the top of a file and the helpers below it, and it is legitimate, as long as the helpers are declarations and not expressions.
Function expressions do not
A function expression assigned to a variable follows the rules of that variable's keyword. With var, the name exists as undefined until the assignment line runs, so a call before it fails with a message that confuses beginners.
try {
greet();
} catch (error) {
console.log(error.name, error.message);
}
var greet = function () {
return "hello";
};
try {
shout();
} catch (error) {
console.log(error.name, error.message);
}
const shout = () => "HEY";
console.log(greet(), shout());TypeError greet is not a function
ReferenceError Cannot access 'shout' before initialization
hello HEYThe first failure says greet is not a function, because the name exists and holds undefined, and calling undefined is a TypeError. The second says it cannot access shout before initialization, because a const is in its dead zone, and that is a ReferenceError. The two messages tell you which keyword you used. Arrow functions and function expressions must be defined before they are called, and const makes that rule loud.
var starts as undefined
A hoisted var exists for the whole function, and reads before the assignment give undefined. Only the declaration is hoisted. The assignment stays in place.
function report() {
console.log(status);
var status = "ready";
console.log(status);
}
report();
// What the engine effectively sees:
function reportEquivalent() {
var status;
console.log(status);
status = "ready";
console.log(status);
}
reportEquivalent();undefined
ready
undefined
readyThe two functions behave the same, and the second is the mental model that gets called "moving to the top". It is a useful picture for var and a wrong one for the other kinds, because a let is not initialized to anything. The name exists from the top, and using it before its line throws.
let, const and class are hoisted too
It is a common claim that let and const are not hoisted. They are. The proof is that an inner declaration blocks access to an outer name for the whole block, even above the declaration line.
const label = "outer";
{
try {
console.log(label);
} catch (error) {
console.log(error.name, error.message);
}
const label = "inner";
}
try {
new Report();
} catch (error) {
console.log(error.name, error.message);
}
class Report {}
console.log(typeof new Report());ReferenceError Cannot access 'label' before initialization
ReferenceError Cannot access 'Report' before initialization
objectIf the inner const label were not hoisted, the first console.log would find the outer label and print outer. It throws instead, because the inner name already exists in the block and is not yet initialized. A class declaration behaves the same, so new Report() above its line fails. The post on the temporal dead zone defines the region and shows its edge cases.
When a function and a var share a name
A function declaration wins at the start, and the var assignment replaces it when that line runs.
function legacy() {
const first = typeof thing;
var thing = "a string";
function thing() {
return "a function";
}
return [first, typeof thing];
}
console.log(legacy());[ 'function', 'string' ]At first thing is the function, because functions are initialized with their body and var thing does not overwrite it at hoisting time. After the assignment line runs, it is a string. The example lives inside a function on purpose: at the top level of an ES module, a var and a function declaration with one name are a SyntaxError, because modules treat top-level function declarations as lexical. This is an interview favorite and almost never appears in good code. It matters because it explains output from legacy files that reuse names, and it is a reason to enable ESLint's no-redeclare and no-use-before-define.
Imports are hoisted
import declarations are hoisted to the top of the module, and the imported modules run before any of the importing module's own code, whatever the position of the import line. A side effect in an imported file therefore happens first.
// main.mjs
console.log("main runs second");
import "./setup.mjs";
// setup.mjs
console.log("setup runs first");Running node main.mjs prints setup runs first and then main runs second, even though the import line sits below the console.log. Put imports at the top for readability, because the engine will treat them that way anyway. The post on ES modules covers the loading phases.
Is hoisting good or bad
The feature is neutral, and what you do with it matters. Using function declarations above their definitions is a style choice some teams like. Relying on var hoisting to read a variable before it is set is always a bug waiting to happen. And a let or const used before its line is an error, which is the language doing you a favor.
Our rule is short: declare before use for variables and classes, use function declarations for top-level named functions where order does not matter, and let the linter enforce it.
// eslint.config.js
export default [
{
rules: {
"no-use-before-define": ["error", { functions: false, classes: true, variables: true }],
},
},
];The functions: false option permits calling a function declaration above its definition, which is the one hoisting behavior that is harmless. Variables and classes must come first.
Where the move-to-the-top picture fails
The picture breaks in three places. It suggests that let becomes undefined at the top, and it does not. It suggests that code is physically reordered, which affects nothing but explains a tidy answer in interviews. And it hides the actual unit of work, the scope, which is why hoisting is per scope: a var in a function is hoisted to that function's top and not to the module's.
The accurate model is the one at the top of this post: the engine creates the bindings of a scope when it enters it, each in its initial state, and then runs the code. Keep that model, and every case in this post follows without a special rule.
The behavior is defined in the specification as declaration instantiation and block declaration instantiation, and MDN's hoisting glossary page covers the four cases.


