let Statement
Last updated: 2026-09-09
let Statement
let declares a block-scoped variable: it only exists inside the nearest pair of curly braces, cannot be accessed outside them, and cannot be redeclared in the same scope. Introduced in ES6, it is the modern replacement for var.
| Category | Declarations |
|---|---|
| Scope | Block scope |
| Hoisting | Hoisted, but stays in the temporal dead zone until the declaration runs |
| Redeclare | Not allowed in the same scope |
| Reassign | Allowed |
| ES version | ES6 (2015) |
📝 Syntax
let name = value;
⚙️ Parameters
| 变量名 | An identifier: starts with a letter, underscore or dollar sign, is case-sensitive, and cannot be a reserved word. |
|---|---|
| 初始值 | Optional. When omitted the variable holds undefined and can be assigned later. |
Returns:Returns nothing — let is a statement, not an expression.
Mutates the original:No (returns a new value)
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
2
visible inside the block
Error: ReferenceError
💡 Tip A let counter in a loop creates a fresh binding per iteration, so callbacks such as setTimeout capture the value of that iteration. With var they would all see the final value.
⚠️ Warning Redeclaring the same name with let in one scope throws a SyntaxError instead of silently overwriting, which helps you catch naming conflicts early.
🌐 Browser support
| Browser | |||||
|---|---|---|---|---|---|
| let | ✓ v49 | ✓ v12 | ✓ v44 | ✓ v10 | ✓ v36 |
| Supported by all modern browsers | |||||
🏷️ Related Statements
💡 Related Cases
More case studies coming soon — check back later.
📚 Related Tutorials
❓ FAQ
QWhat is the difference between let and var?
AThree things: scope (let is block-scoped, var is function-scoped and may leak onto the global object), redeclaration (let forbids it, var allows it), and hoisting (let keeps the binding in the temporal dead zone and throws, var yields undefined).
QWhy does using a let variable before its declaration throw an error?
AThe span between entering a scope and running the let declaration is called the temporal dead zone (TDZ). The binding exists but is uninitialized, so touching it throws a ReferenceError. This is intentional: it prevents the silent undefined bugs that var allows.
QWhy does let behave differently inside loops?
AWith let, each iteration gets its own copy of the loop variable (carrying over the previous value), so every closure captures its own iteration. With var there is a single shared binding, so callbacks later all observe the last value.