JavaScript interview question

What are the differences between var, let and const, and what is hoisting?

Short answer

var is function-scoped and hoisted with an initial value of undefined. let and const are block-scoped and also hoisted, but they sit in a temporal dead zone until their declaration runs, so accessing them early throws a ReferenceError. const prevents reassignment of the binding, not mutation of the object it points to.

Quick comparison

  • var — function scope, can be redeclared, initialised to undefined when hoisted.
  • let — block scope, cannot be redeclared in the same scope, temporal dead zone.
  • const — like let, but must be initialised and cannot be reassigned.

Hoisting explained

Before running a scope, the engine registers its declarations. Function declarations are hoisted with their body, which is why you can call them before they appear. Variables declared with var exist as undefined; let and const exist but cannot be touched until initialised.

How to answer it in an interview

  • Say “const freezes the binding, not the value” and show a pushed array.
  • Mention that modern code defaults to const and uses let only when reassigning.

Walk into your next interview prepared

Start free, install the Windows app and run a practice session today.