TExcelTAD
Home › JavaScript › JS Let

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

js · try it yourself
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:

js · try it yourself
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:

js · try it yourself
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:

js
let user = "Ana";
let user = "Ben";   // SyntaxError: Identifier 'user' has already been declared

let 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:

js · try it yourself
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):

js · try it yourself
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 const instead of let — it tells the reader "this stays the same".

Test yourself

Where is a variable declared with let inside an if block visible?

  1. Everywhere in the file
  2. In the whole function
  3. Only inside that block
  4. Only on the line it is declared

What happens if you read a let variable before its declaration line?

  1. You get undefined
  2. You get null
  3. A ReferenceError is thrown
  4. It reads the global variable

Why do callbacks created in a for (let i …) loop see different values of i?

  1. let copies values to the heap
  2. Each iteration gets its own binding of i
  3. Callbacks run immediately
  4. It is a browser bug