JavaScript let

A Variable That Can Change

JavaScript let

The `let` keyword declares a block-scoped variable that you can reassign. Use it when a value will change over time.

4 min read Level 1/5 #let#variables#scope
What you'll learn
  • Declare reassignable variables with `let`
  • Understand block scope and the temporal dead zone
  • Know when to choose `let` over `const`

let declares a variable whose value can change after it’s declared. Reach for let when you know the value will be reassigned.

let allows reassignment script.js
let count = 0;
count = count + 1;
count = count + 1;
console.log(count); // 2
▶ Preview: console

Block Scope

A let variable only exists inside the { } it was declared in. Step outside the block, and the name is gone.

Block scope script.js
if (true) {
  let message = "inside the if";
  console.log(message);
}

// console.log(message); // ReferenceError — message is not defined
▶ Preview: console

This is a feature: variables can’t leak out and accidentally interact with code outside the block. It’s the main reason let replaced var.

Use let in for Loops

The classic place you’ll write let:

for loop with let script.js
for (let i = 0; i < 3; i++) {
  console.log(i);
}
// 0
// 1
// 2
▶ Preview: console

Each iteration gets its own i. (With var, the loop variable leaked out of the loop — surprising and a common bug source.)

The Temporal Dead Zone

A let binding exists in its scope from the start of the block, but you can’t use it until the declaration runs. This is called the temporal dead zone.

Temporal dead zone script.js
// console.log(score); // ReferenceError — can't access before declaration
let score = 100;
console.log(score); // 100
▶ Preview: console

When to Choose let

  • The value will change (counters, accumulators, loop indices).
  • You’ll genuinely reassign — not just mutate an object.

For values that won’t change, prefer const.

Up Next

const is let’s sibling — same scope rules, but no reassignment.

JavaScript const →
Reference