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

Beginner

Answer

  • var: Function-scoped, can be redeclared and updated, hoisted with undefined
  • let: Block-scoped, can be updated but not redeclared, hoisted but not initialized
  • const: Block-scoped, cannot be updated or redeclared, must be initialized at declaration
var a = 1;     // Function scoped
let b = 2;     // Block scoped
const c = 3;   // Block scoped, immutable binding