Skip to solution
easyPhone Screen

What is hoisting in JavaScript?

944 views
01

Understand the problem

Question presented to candidate: "What is hoisting in JavaScript, and how does it actually behave differently for var, let, const, function declarations, and class declarations?"

What a strong answer should cover:

  • Hoisting means the engine registers every declared name in a scope before executing any code in that scope -- the name exists from the top, even though source reads top to bottom.
  • var declarations are hoisted AND initialized to undefined immediately, so reading a var before its declaration line returns undefined rather than throwing.
  • let, const, and class declarations are hoisted but left uninitialized in the temporal dead zone (TDZ) -- reading any of them before their own declaration line throws a ReferenceError, not undefined.
  • function declarations are hoisted completely, including their body, so they are callable even before the line they are written on; a function EXPRESSION assigned to a var only hoists the var itself (as undefined), never the assignment.
  • Real motivation to state clearly: hoisting is not "moving code around" -- the engine does a scope-analysis pass first, and each declaration form reacts differently to being read before its own line actually runs.

Clarifying questions expected:

  • "Do you want me to also cover class hoisting and the TDZ specifically, or keep this to var versus let and const?"

Code / implementation expected: Yes -- a short, runnable snippet demonstrating each hoisting behavior side by side.

hoistingscopevariablesfunctions
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interview questions. Difficulty: Easy

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every result below was actually run on Node.js v24.19.0 — the printed output is re

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSHoisting across var, let, function declaration, function expression, and class (run directly)
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 10 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track