← Back to JavaScript, page 1

javascript-0001 · Easy fresher

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

var is function-scoped, can be redeclared, and is hoisted with the value undefined. let and const are block-scoped and sit in the temporal dead zone until their declaration runs. const also forbids reassigning the binding, though the object it points to can still change.

Full answer

A scope is the part of the program where a name can be used. The three keywords differ in how they define that part.

var belongs to the nearest function, or to the global scope if it is outside any function. An if block or a for loop does not create a new scope for it. A var name can be declared twice in the same scope without an error, and the declaration is hoisted to the top of its function with the starting value undefined.

let and const belong to the nearest pair of curly braces. They are hoisted too, but they cannot be touched before the line that declares them. The gap between the start of the scope and that line is called the temporal dead zone, and reading the name there throws a ReferenceError. Declaring the same let or const name twice in one scope is a SyntaxError.

const adds one more rule: the binding cannot be assigned again, and it must be given a value when it is declared. This protects the variable name, not the value. A const array can still be pushed to, and a const object can still have its properties changed.

A simple picture is a classroom. A var is a notice on the school wall that every room can read. A let is a note on one desk that only that desk can see.

javascript
var marks = 40;
var marks = 55; // allowed: same var name declared twice
console.log('var marks:', marks);

if (true) {
  var leakyName = 'visible after the block';
  let blockName = 'only inside the block';
  console.log('inside:', blockName);
}
console.log('var after block:', leakyName);
console.log('let after block:', typeof blockName);

try {
  console.log(late);
  let late = 10;
} catch (error) {
  console.log('let before declaration:', error.name);
}

const subjects = ['Maths'];
subjects.push('Physics'); // the array changes, the binding does not
console.log('const array:', subjects);

try {
  subjects = [];
} catch (error) {
  console.log('reassign const:', error.name);
}

Run with Node.js v24.22.1 as js-1.js; two runs gave identical output. Output: var marks: 55 inside: only inside the block var after block: visible after the block let after block: undefined let before declaration: ReferenceError const array: [ 'Maths', 'Physics' ] reassign const: TypeError

Key points

  • var is function-scoped; let and const are block-scoped.
  • Reading a let or const name before its declaration throws a ReferenceError (temporal dead zone); a var gives undefined.
  • Redeclaring a var is allowed; redeclaring a let or const in the same scope is a SyntaxError.
  • const blocks reassignment of the name but does not freeze the object it refers to.
  • Modern code uses const by default and let only when the value must change.

Common mistakes

  • Saying that let and const are not hoisted. They are hoisted but stay uninitialized in the temporal dead zone.
  • Believing a const object is immutable. Only the binding is fixed; use Object.freeze for a shallow lock on the object.
  • Using var inside a for loop and expecting each iteration to get its own copy.
  • Writing typeof name as a safe check for a let name in the temporal dead zone; it throws there.

Follow-ups

  • Why does a closure created in a for loop with var print the same number every time, and how does let change that?
  • What does typeof return for an undeclared name, and for a name in the temporal dead zone?
  • How do top-level var and let behave differently on the global object in a browser script?

Tips

  • Start with scope, then hoisting, then reassignment. That order follows how most interviewers probe the topic.
  • Say "temporal dead zone" and explain it in one sentence; it shows you know hoisting is not just "moving code up".
  • Mention a team habit such as const by default, let when needed, no var, and name the lint rule (no-var, prefer-const) that enforces it.