JavaScript Let
let declares a variable that can be reassigned and that lives only inside the block where it was declared. It was introduced in ES2015 to replace most uses of var.
Reassigning a let variable
let score = 0;
score = score + 10;
score = score + 5;
console.log(score);15
Block scope
A block is any code between { and } — the body of an if, a loop, or just a bare pair of braces. A let variable declared inside a block doesn't exist outside it:
const hour = 9;
if (hour < 12) {
let greeting = "Good morning";
console.log(greeting);
}
try {
console.log(greeting);
} catch (err) {
console.log(err.message);
}Good morning
greeting is not defined
A variable with the same name inside a block is a different variable that temporarily hides (shadows) the outer one:
let label = "outer";
{
let label = "inner";
console.log(label);
}
console.log(label);inner
outer
No redeclaration
You can't declare the same name twice with let in the same scope — a safety net that var doesn't have:
let user = "Ana";
let user = "Ben"; // SyntaxError: Identifier 'user' has already been declaredlet in loops
Each iteration of a for loop gets its own copy of a let counter. That matters when the loop creates functions that run later:
const printers = [];
for (let i = 1; i <= 3; i++) {
printers.push(() => console.log("let:", i));
}
printers.forEach((p) => p());
const legacy = [];
for (var j = 1; j <= 3; j++) {
legacy.push(() => console.log("var:", j));
}
legacy.forEach((p) => p());let: 1
let: 2
let: 3
var: 4
var: 4
var: 4
With var there is just one j shared by all iterations, and by the time the functions run, it has reached 4.
The temporal dead zone
A let variable exists from the start of its block, but you can't touch it until the line that declares it has run. The gap is called the temporal dead zone (TDZ):
try {
console.log(total);
} catch (err) {
console.log(err.name + ": " + err.message);
}
let total = 100;
console.log(total);ReferenceError: Cannot access 'total' before initialization
100
With var, the same code would quietly print undefined — a classic source of bugs. The TDZ turns that mistake into a clear error.
let vs var at a glance
let |
var |
|
|---|---|---|
| Scope | Block { } |
Whole function |
| Redeclare in same scope | ❌ SyntaxError | ✅ silently allowed |
| Use before declaration | ❌ ReferenceError (TDZ) | ✅ gives undefined |
| New binding per loop iteration | ✅ | ❌ |
| Becomes a property of the global object | No | Yes (in classic scripts) |
Tip: If a variable never changes after it's set, use
constinstead oflet— it tells the reader "this stays the same".
Test yourself
Where is a variable declared with let inside an if block visible?
- Everywhere in the file
- In the whole function
- Only inside that block
- Only on the line it is declared
What happens if you read a let variable before its declaration line?
- You get undefined
- You get null
- A ReferenceError is thrown
- It reads the global variable
Why do callbacks created in a for (let i …) loop see different values of i?
letcopies values to the heap- Each iteration gets its own binding of
i - Callbacks run immediately
- It is a browser bug