Skip to main content

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

Answer

Featurevarletconst
ScopeFunctionBlockBlock
HoistingYes (undefined)Yes (TDZ)Yes (TDZ)
Reassign
Redeclare

Example

var a = 1;
var a = 2; // OK

let b = 1;
// let b = 2; // Error: already declared

const c = 1;
// c = 2; // Error: assignment to constant

Key Points

  • Use const by default
  • Use let when you need to reassign
  • Avoid var in modern JavaScript