JavaScriptInterview preparation
Foundation·Concept·2 min read

What is the difference between var, let and const?

Practice this question

Definition:

var, let and const declare variables, but they have different scope and initialization rules. let and const are block-scoped, making it easier to keep a variable inside the code that needs it. const prevents reassignment of the binding; it does not make an object immutable.

Key points:

  • var: function scope; reading before its declaration gives undefined.

  • let: block scope; can be reassigned.

  • const: block scope; cannot be reassigned, but object contents can change.

  • Use const by default when the binding will not change, and let when reassignment is needed.

  • Redeclaring a let or const in the same scope is an error; var allows many redeclarations and can hide mistakes.

Comparison:

Pointvarletconst
Scope inside functionsFunctionBlockBlock
ReassignYesYesNo
Redeclare in the same scopeUsually allowedNoNo
Read before initializationundefinedReferenceErrorReferenceError

Example:

JavaScript
const user = { name: 'Asha' };
user.name = 'Neha'; // Allowed: changes the object.
// user = {};      // Not allowed: reassigns the binding.

Interview point:

  • Always distinguish changing an object's properties from assigning a different object to the variable.
Adapted from 01_JAVASCRIPT_TYPESCRIPT.md · “What is the difference between var, let and const?”. Code examples are study fragments. Difficulty and core labels are editorial estimates; version-specific behavior should be checked against your project.