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.
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 count = 0;
count = count + 1;
count = count + 1;
console.log(count); // 2 Block Scope
A let variable only exists inside the { } it was declared in.
Step outside the block, and the name is gone.
if (true) {
let message = "inside the if";
console.log(message);
}
// console.log(message); // ReferenceError — message is not defined 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 (let i = 0; i < 3; i++) {
console.log(i);
}
// 0
// 1
// 2 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.
// console.log(score); // ReferenceError — can't access before declaration
let score = 100;
console.log(score); // 100 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.