Skip to content
Tutorials Center
My learning

Topics

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.

JavaScript
let total = 0;
let a = 1, b = 2, c;

Try it

JS Block scope

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS The temporal dead zone

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
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 let variable is visible only in its block, including nested blocks, and an inner let with 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 let line throws a ReferenceError. This is deliberate: it exposes use-before-define bugs instead of returning undefined. Even typeof throws 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...of and for...in with let work the same way.
  • No global property. A top-level let in a script is global in reach but is not a property of globalThis.
  • Switch cases. All case clauses 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 const by default; it tells readers the binding never changes. Choose let when you reassign, for example in accumulators.
  • Uninitialized is allowed for let (let x;), but not for const.

Browser support

Supported in every current browser and in Node.js; it is part of the language since ES2015.