📦 JavaScript

var vs let vs const in JavaScript — Which and When

📅 Jun 11, 2026 ⏱ 4 min read

Three keywords, one decision rule: const by default, let when you must reassign, var never. Here is why.

The differences

The classic trap

for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i));  // 3, 3, 3  😱
}
for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i));  // 0, 1, 2  ✅ new binding per loop
}

const nuance interviewers probe

const arr = [1, 2];
arr.push(3);      // ✅ fine — contents can change
arr = [9];        // ❌ TypeError — the BINDING is constant

const prevents reassignment, not mutation. Freeze contents with Object.freeze(). Full lesson: Variables.

← All Articles