Skip to the solution
EasyFrontend6 min read

What is the difference between var, let, and const?

1.2k views

The problem

Question presented to candidate: "What's the actual difference between var, let, and const β€” not just 'let is block-scoped' β€” and can you show me a real bug that happens if you use var inside a loop with a callback?"

What a strong answer should cover:

  • πŸ“Œ Interview term: function scope (var) β€” a var declared anywhere inside a function is accessible throughout the entire function, ignoring block boundaries like if or for. Verified directly: a var declared inside an if block is still readable after the block ends.
  • πŸ“Œ Interview term: block scope (let/const) β€” a let/const is only accessible within the nearest enclosing block ({}). Verified directly: accessing it outside the block throws a real ReferenceError.
  • πŸ“Œ Interview term: the Temporal Dead Zone (TDZ) β€” let/const are hoisted to the top of their scope like var, but remain uninitialized until their declaration line executes; accessing them before that point throws a real ReferenceError ("Cannot access before initialization"), distinct from var, which is hoisted AND initialized to undefined β€” verified directly, a real, observable difference.
  • πŸ“Œ Interview term: the classic var-in-loop-closure bug β€” verified directly with real setTimeout callbacks: a for (var i ...) loop's callbacks all see the SAME final value of i (because var has one shared binding for the whole loop), while for (let i ...) gives each callback its OWN per-iteration binding, seeing the value at the time of its own iteration.
  • A precise answer names that const prevents reassignment of the binding, not mutation of the value β€” verified directly: reassigning a const throws, but mutating a const-bound object's properties succeeds fine.

Clarifying questions expected:

  • None β€” this is a definitional/comparison question; producing the real closure bug live is the strongest possible signal.

Code / implementation expected: Yes β€” reproducing the var-vs-let loop-closure bug with real setTimeout callbacks is the single most convincing demonstration of genuine understanding.

  • var
  • let
  • const

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal scope, TDZ, and the classic var-vs-let loop-closure bug reproduced with setTimeout

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 3 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

How does Array.prototype.with() let you replace one array element immutably, and where would that pattern actually help?

Continue