LOGBOOK

HELP

Quiz Entry - updated: 2026.07.14

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

let is a reassignable block-scoped variable, const is a block-scoped variable you can't reassign, and var is the old function-scoped keyword best avoided.

These three keywords all declare variables but differ in scope and what you're allowed to do afterwards:

Keyword Scope Reassignable? Redeclarable?
let Block { } Yes No
const Block { } No No
var Function Yes Yes
let x = 1;
x = 2;          // OK — let can be reassigned
// let x = 3;   // Error — can't redeclare in the same scope

const y = 1;
// y = 2;       // Error — const can't be reassigned

var z = 1;
var z = 2;      // allowed (but confusing — this is why var causes bugs)

Practical guidance:

  • Use const by default — it signals "this won't change" and catches accidental reassignment.
  • Use let only when you genuinely need to reassign the variable.
  • Avoid var — its function scope (rather than block scope) leads to subtle bugs where a variable leaks out of the block you thought contained it.

One subtlety: const makes the binding constant, not the value's contents. You can't reassign a const object to a new object, but you can still change that object's properties (const p = {a:1}; p.a = 2; is fine).

From Quiz: WEBT / Introduction to JavaScript | Updated: Jul 14, 2026