JavaScript Reference ยท Statements and Declarations
The let statement
Reference for the JavaScript let declaration: block scope, the temporal dead zone, no redeclaration, per-iteration loop bindings and destructuring.
The let declaration creates a variable that exists only inside the block ({ ... }, a loop body, a switch case block, or the module or script it is in). Unlike var, it cannot be used before its declaration line, and the same name cannot be declared twice in the same scope. You can reassign it as often as you like.
Use let for values that really change, such as counters and accumulators, and const for everything else.
let total = 0; let a = 1, b = 2, c;
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
let name1; let name1 = value1; let name1 = value1, name2 = value2, nameN; let { a, b } = object; let [first, second] = list;
| Part | Description |
|---|---|
name |
A valid identifier (not let itself) or a destructuring pattern. |
value |
Any expression. Without an initializer the variable starts as undefined. |
Exceptions: a SyntaxError if the name is already declared with let, const, class or a parameter in the same scope; a ReferenceError if you read it before its declaration has run.
Usage notes
- Block scope. A
letvariable is visible only in its block, including nested blocks, and an innerletwith the same name shadows the outer one. - Temporal dead zone (TDZ). The name is known from the start of the block, but touching it before the
letline throws aReferenceError. This is deliberate: it exposes use-before-define bugs instead of returningundefined. Eventypeofthrows inside the TDZ. - No redeclaration in the same scope. Redeclaring in an inner block is fine (that is shadowing).
- Loops.
for (let i = ...)makes a fresh binding for each iteration, so closures created in the loop capture the value of that iteration.for...ofandfor...inwithletwork the same way. - No global property. A top-level
letin a script is global in reach but is not a property ofglobalThis. - Switch cases. All
caseclauses share one block; wrap a case body in braces when you declare variables, or two cases declaring the same name will collide. - let versus const. Choose
constby default; it tells readers the binding never changes. Chooseletwhen you reassign, for example in accumulators. - Uninitialized is allowed for
let(let x;), but not forconst.
Browser support
Supported in every current browser and in Node.js; it is part of the language since ES2015.