EasyConcept

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

Technology:
JavaScript
Experience:
Intern,
Junior

Quick answer

var is function-scoped and can be redeclared; let and const are block-scoped, and const cannot be reassigned after it is set.

Detailed explanation

var is function-scoped, is hoisted and initialised to undefined, and can be redeclared in the same scope. let and const are block-scoped: they only exist inside the nearest pair of curly braces.

let and const are also hoisted, but they stay in the temporal dead zone until the line where they are declared, so using them earlier throws a ReferenceError instead of silently giving undefined.

const prevents reassigning the variable, not mutating the value. A const array or object can still be changed by pushing to it or setting a property. Use const by default, let when you need to reassign, and avoid var in new code.

Example

javascript
if (true) {
  var a = 1;
  let b = 2;
}
console.log(a); // 1  (var ignores the block)
console.log(b); // ReferenceError: b is not defined

const list = [1, 2];
list.push(3);   // allowed, the array is mutated
list = [];      // TypeError: Assignment to constant variable

How to explain it in an interview

I would say: var is function-scoped and hoisted as undefined, while let and const are block-scoped and sit in the temporal dead zone until their declaration runs. const stops reassignment, not mutation, so a const array can still be pushed to. In practice I use const by default, let when a value must change, and avoid var.

Key points

  • var: function scope, hoisted as undefined, can be redeclared
  • let: block scope, temporal dead zone, cannot be redeclared in the same scope
  • const: like let, but the binding cannot be reassigned

Common mistakes

  • Saying const makes objects immutable. It only prevents reassigning the variable; use Object.freeze for shallow immutability.
  • Claiming let and const are not hoisted. They are hoisted but not initialised, which is why early access throws a ReferenceError.
  • Forgetting the classic loop bug: var in a for loop shares one binding, so callbacks all see the final value.

Follow-up questions

  • What is the temporal dead zone?
  • Why does a var inside a for loop behave differently with setTimeout?
  • JavaScriptEasy

    What is hoisting in JavaScript?

    Hoisting is JavaScript moving declarations to the top of their scope during compilation, so functions and var variables can be referenced before the line where they are written.

    Intern · Junior · Functions & Scope
  • JavaScriptMedium

    What is a closure in JavaScript and why is it useful?

    A closure is a function that keeps access to the variables of the scope where it was created, even after that outer function has finished running.

    Junior · Mid-level · Functions & Scope
  • JavaScriptMedium

    How does the this keyword work in JavaScript?

    this is decided by how a function is called, not where it is written: as a method it is the object before the dot, with new it is the new object, and arrow functions inherit it from the enclosing scope.

    Junior · Mid-level · Functions & Scope