ES6+ Modern JavaScript

let / const vs var

Understanding variable declarations is fundamental to JavaScript. ES2015 introducedlet andconst to fix the scoping, hoisting, and closure pitfalls ofvar.

What is the difference between let, const and var?

let and const are block-scoped (they only exist inside the nearest { }) and can't be used before their declaration. const also can't be reassigned. var is function-scoped, hoisted as undefined, can be redeclared, and leaks out of if and for blocks. Modern code uses const by default, let when reassigning, and never var.

if (true) {
  var a = 1;
  let b = 2;
}
a; // 1   (var leaked out of the block)
b; // ReferenceError

for (var i = 0; i < 3; i++) setTimeout(() => console.log(i)); // 3 3 3
for (let j = 0; j < 3; j++) setTimeout(() => console.log(j)); // 0 1 2

Quick Comparison

Featurevarletconst
ScopeFunction / GlobalBlockBlock
HoistedYes (undefined)Yes (TDZ)Yes (TDZ)
Re-declarationAllowedNot allowedNot allowed
Re-assignmentAllowedAllowedNot allowed
Global object propertyYes (in browser)NoNo
Use in for loopShared binding (bug)New binding per iterNew binding per iter

Temporal Dead Zone (TDZ)

let and const are hoisted to the top of their block but not initialized. Accessing them before their declaration throws a ReferenceError.

// TDZ starts here for 'name'
console.log(name); // ReferenceError!
let name = 'Alice'; // TDZ ends
console.log(name); // 'Alice'

const is Not Deeply Immutable

const prevents re-assigning the binding, but the object it points to is still mutable. Use Object.freeze() for shallow immutability.

const user = { name: 'Alice' };
user.name = 'Bob'; // OK ✓
user = {}; // TypeError ✗
const frozen = Object.freeze({ x: 1 });
frozen.x = 99; // Silently ignored

Best Practices

Default to const

Use const for everything. It communicates intent and prevents accidental re-assignment.

Upgrade to let only when needed

Use let only when you genuinely need to re-assign (loop counters, accumulators, state machines).

Never use var in new code

var's function scope and hoisting behaviour lead to subtle bugs. Use let/const exclusively.

Declare variables at the top of their block

Even though TDZ applies, declaring early makes the code easier to reason about.

Common Pitfalls

var in for loops — shared binding causes closure bugs
Use let — each iteration gets its own binding
Assuming const makes objects immutable
Use Object.freeze() or immutable libraries for true immutability
Using var in if blocks expecting block scope
var bleeds out of if/for/while — always use let/const
Accessing let/const before declaration (TDZ)
Declare variables before use; move declarations to top of block

Interactive Examples

Code Editor

<button id="run">Run</button>
<pre id="out"></pre>

Preview

let, const, and var — step by step

1 / 4
1

var — function scoped and hoisted

var is scoped to the enclosing function (or global if outside any function). It is hoisted to the top of its scope and initialized to undefined — meaning you can reference it before the declaration without a ReferenceError, you just get undefined. Avoid var in modern code: block scope is almost always what you actually want.

1 of 4
💡Default to const, use let when reassignment is needed
Prefer const for every variable unless you know you will reassign it. This signals immutable intent, prevents accidental reassignment bugs, and lets JavaScript engines make certain optimizations. Upgrade to let only when you explicitly need to reassign. Avoid var entirely in modern code.

🎯 Quick Check

What happens if you access a let variable before its declaration in the same scope?

let vs const vs var: summary

Key points

  • const objects and arrays can still be modified; only the binding is constant.
  • Top-level var creates a property on window; let and const don't.
  • Redeclaring a let in the same scope is a syntax error, which catches typos early.

Frequently asked questions

›Why does the var loop print 3 3 3?

var i is one shared variable for the whole function. By the time the callbacks run, the loop has finished and i is 3. let creates a new j for each iteration.