← Back to JavaScript, page 1

javascript-0011 · Medium mid

What is hoisting in JavaScript, and how does it differ for var, let, const, functions, and classes?

Before running a scope, the engine creates every declared name in it, so names exist before their line is reached. var is set to undefined, function declarations are fully available, and let, const, and class stay uninitialized in the temporal dead zone. Only declarations are hoisted, never assignments.

Full answer

Hoisting is a side effect of how the engine prepares code. Before a function or script runs, the engine scans it and registers all declared names in the scope. Nothing is physically moved, but the result behaves as if declarations were lifted to the top.

What happens to each kind of declaration differs:

  • var is registered and immediately set to undefined. Using it early gives undefined, not an error.
  • A function declaration is registered with its full body, so it can be called before the line where it appears.
  • let, const, and class are registered but not initialized. Touching them before their line throws a ReferenceError. This period is the temporal dead zone.
  • A function expression, such as const run = function () {}, follows the rule of the variable that holds it. With var it is undefined at first, so calling it early gives a TypeError; with const it throws a ReferenceError.

Only the declaration is hoisted. In var total = 5, the name is hoisted and the value 5 is assigned when the line runs.

If a function and a var share a name, the function wins at the start, and the later assignment to the variable can replace it.

Knowing this explains many puzzle questions, but good code avoids relying on it: declare names before use.

javascript
console.log('var before:', hoistedVar);
var hoistedVar = 'ready';

console.log('function before:', greet('Tara'));
function greet(name) {
  return `Hello, ${name}`;
}

try {
  console.log(expression());
} catch (error) {
  console.log('function expression with var:', error.name);
}
var expression = function () {
  return 'never reached';
};

try {
  console.log(Klass);
} catch (error) {
  console.log('class before declaration:', error.name);
}
class Klass {}
console.log('class after declaration:', typeof Klass);

var shadow = 'outer';
function test() {
  console.log('shadow inside:', shadow); // inner var is hoisted and hides the outer one
  var shadow = 'inner';
}
test();

Run with Node.js v24.22.1 as js-11.js; two runs gave identical output. Output: var before: undefined function before: Hello, Tara function expression with var: TypeError class before declaration: ReferenceError class after declaration: function shadow inside: undefined

Key points

  • Declarations are registered when the scope is created; assignments run in order.
  • var starts as undefined; let, const, and class are in the temporal dead zone until declared.
  • Function declarations are hoisted with their bodies; function expressions are not.
  • Calling an undefined var-held function gives TypeError, not ReferenceError.
  • Hoisting works per scope, not globally.

Common mistakes

  • Saying hoisting moves code to the top. The code stays where it is; the names are created early.
  • Saying let and const are not hoisted. A let name in the temporal dead zone still blocks access to an outer variable of the same name.
  • Expecting a function expression to be callable before its line.
  • Forgetting that a var inside a function hides an outer variable for the whole function, which causes the shadow example.

Follow-ups

  • What does typeof return for a let variable in the temporal dead zone?
  • What if a function declaration appears inside an if block?
  • Why do default parameter expressions also have a temporal dead zone?

Tips

  • Describe the two phases: creation (declarations registered) and execution (code runs line by line).
  • Work through the shadow example aloud; it is a common live question.
  • State that you avoid depending on hoisting in real code, then explain what lint rules help.