JS Let
In this page:
let variableName = value;
variableName = newValue;
Block Scope
A let variable only exists inside the nearest set of curly braces { } that contains it, whether that's an if statement, a loop, or any other code block, and is completely inaccessible outside that block.
Example: Block Scope
if (true) {
let message = "Inside the block";
console.log(message);
}
// console.log(message); // ReferenceError: message is not defined
No Redeclaration
JavaScript does not allow declaring the same let variable name twice within the same scope, this restriction helps catch accidental naming conflicts that could otherwise cause confusing bugs.
Example: No Redeclaration
let name = "Sam";
// let name = "Alex"; // SyntaxError: already declared
name = "Alex"; // reassignment is fine
console.log(name);
The Temporal Dead Zone
The temporal dead zone is the period between the start of a block and the actual line where a let variable is declared, during which the variable technically exists but cannot be accessed, and doing so throws an error.
Example: The Temporal Dead Zone
// console.log(value); // ReferenceError: Cannot access before initialization
let value = 10;
console.log(value);
let Inside Loops
Because let is block-scoped, each iteration of a loop gets its own fresh copy of a let variable, which matters a lot when working with functions created inside a loop, like event handlers or timers.
Example: let Inside Loops
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 0);
}
// Logs 0, 1, 2 - each iteration gets its own i
When to Use let vs var
In modern JavaScript, let should be your default for any value that changes, its block scoping and stricter rules prevent entire categories of bugs that var allows, making code more predictable across a whole team.
Example: When to Use let vs var
// Declare the variable `total`, set to `0`
// Declare the variable `total`, set to `0`
let total = 0;
// Classic for-loop: `let i = 1; i <= 3; i++`
// Classic for-loop: `let i = 1; i <= 3; i++`
for (let i = 1; i <= 3; i++) {
total += i;
}
// Print `total` to the console
// Print `total` to the console
console.log(total);
- Trying to access a let variable outside the block where it was declared, which throws a ReferenceError.
- Attempting to declare the same let variable twice in the same scope, which is not allowed and causes an error.
- Accessing a let variable before its declaration line runs, falling into the temporal dead zone and triggering an error.
- let creates block-scoped variables, only accessible within the { } block where they were declared.
- Unlike var, let does not allow redeclaring the same variable name in the same scope.
- let is the modern default for any variable whose value is expected to change over the life of the script.
let has been supported in all major browsers since 2015 and is considered a safe, standard part of modern JavaScript today.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: