Table of Contents

JavaScript let keyword is one of the most important features introduced in ECMAScript 2015 (ES6). It provides a modern way to declare variables in JavaScript and offers better control over variable scope compared to the traditional var keyword.

Before ES6, JavaScript developers primarily used var to declare variables. However, var has function scope rather than block scope, which can sometimes lead to unexpected behavior. The let keyword solves many of these problems by introducing block-scoped variables.

Using let, developers can declare variables whose values can be changed later. It is particularly useful when working with loops, conditional statements, functions, and modern JavaScript applications.

Syntax and Declaration Forms

The let keyword supports several declaration patterns, all of which follow the same scoping and hoisting rules.

Basic Declaration

A variable can be declared without initialization, in which case it holds undefined until assigned:

javascript
let myVariable;
console.log(myVariable);   // undefined

myVariable = 123;
console.log(myVariable);   // 123

Explicit Initialization

Declaring and assigning in a single statement is the most common and recommended form:

javascript
let pokerChips = 100;
let userName = "Alice";
let isActive = true;

Multiple Declarations

A single let statement can declare multiple variables, separated by commas. Initializers for later variables can reference earlier ones in the same list:

javascript
let a = 1, b = 2, c = 3;
let x = 10, y = x * 2;   // y = 20

The list following let is called a binding list; the commas are not the comma operator, and the = signs are not assignment operators in the usual expression sense .

Block Scoping: The Core Advantage

The most significant difference between let and var is how they handle scope. A var declaration is function-scoped: it is visible throughout the entire function in which it is declared, even inside nested blocks. A let declaration is block-scoped: it is visible only within the block where it is declared, and any nested sub-blocks .

Block Scope in Action

javascript
{
  let blockScoped = "inside";
  console.log(blockScoped);   // "inside"
}

// console.log(blockScoped);   // ReferenceError: blockScoped is not defined

With var, the variable would leak out of the block:

javascript
{
  var functionScoped = "inside";
}
console.log(functionScoped);   // "inside" (leaked!)

let in Loops

The block scoping of let is especially valuable in for loops. When let is used in the loop header, each iteration creates a new binding, preventing the classic closure bug that plagued var-based loops .

javascript
// With let: each iteration captures its own value
for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 100);
}
// Logs: 0, 1, 2

// With var: all callbacks share the same variable
for (var j = 0; j < 3; j++) {
  setTimeout(() => console.log(j), 100);
}
// Logs: 3, 3, 3

The let version works correctly because each loop iteration has its own i binding, while var hoists j to the function scope where all callbacks reference the same final value.

let in switch Statements

All case clauses within a switch statement share the same block. Declaring a let variable in one case makes it visible (though uninitialized) in others, which can cause SyntaxError if redeclared or ReferenceError if accessed before initialization :

javascript
switch (value) {
  case 1:
    let result = "one";
    break;
  case 2:
    // let result = "two";   // SyntaxError: Identifier 'result' has already been declared
    console.log(result);     // ReferenceError if case 1 never ran
    break;
}

Wrapping each case in its own block {} avoids this:

javascript
switch (value) {
  case 1: {
    let result = "one";
    break;
  }
  case 2: {
    let result = "two";
    break;
  }
}

The Temporal Dead Zone (TDZ)

let declarations are hoisted, but unlike var, they are not initialized during hoisting. The period between the start of the block and the point where the declaration is executed is called the Temporal Dead Zone (TDZ). Accessing a let variable during its TDZ throws a ReferenceError .

javascript
{
  // TDZ for `foo` starts here
  console.log(foo);   // ReferenceError: Cannot access 'foo' before initialization

  let foo = 2;        // TDZ ends here
  console.log(foo);   // 2
}

Compare with var, which returns undefined when accessed before declaration:

javascript
{
  console.log(bar);   // undefined (no error)
  var bar = 1;
}

The term “temporal” is used because the TDZ depends on the order of execution (time), not the order in which code is written (position). A function that references a let variable is safe as long as it is called after the variable’s declaration, even if the function is defined before :

javascript
{
  const func = () => console.log(letVar);   // OK: function defined before declaration

  let letVar = 3;   // TDZ ends here
  func();           // 3 (called outside TDZ)
}

Redeclaration Restrictions

let cannot be redeclared within the same scope. This applies to redeclaration by any other declaration form, including let, const, class, function, var, and import .

javascript
let x = 1;
// let x = 2;        // SyntaxError: Identifier 'x' has already been declared
// var x = 3;        // SyntaxError
// const x = 4;      // SyntaxError

However, shadowing in an inner block is allowed because the inner declaration belongs to a different scope:

javascript
let x = 1;
{
  let x = 2;   // Allowed: different block scope
  console.log(x);   // 2
}
console.log(x);   // 1

This restriction makes code more predictable by eliminating the “accidental redeclaration” pattern that var permitted, where a variable could be silently overwritten in the same scope.

let and the Global Object

A subtle but important difference: when declared at the top level of a script, let does not create a property on the global object (globalThis, window in browsers). var does .

javascript
var globalVar = "visible";
let globalLet = "hidden";

console.log(window.globalVar);   // "visible"
console.log(window.globalLet);   // undefined

This behavior prevents let declarations from polluting the global namespace, which is one reason modern style guides favor it over var.

let vs const: When to Use Which

const and let share identical block-scoping and TDZ semantics. The only difference is reassignment: const forbids it, while let allows it .

MDN and most modern style guides recommend the following principle of least privilege:

  • Use const by default for variables that are never reassigned. This makes intent clear and prevents accidental mutation.

  • Use let only when the variable’s value genuinely needs to change (loop counters, accumulators, state variables that are reassigned).

javascript
const PI = 3.14159;          // Never reassigned
let counter = 0;             // Will be reassigned
counter += 1;

Note that const does not make objects immutable—it only prevents reassignment of the binding. The object’s properties can still be mutated:

javascript
const user = { name: "Alice" };
user.name = "Bob";          // Allowed
// user = { name: "Cara" }; // TypeError: Assignment to constant variable

Categorized in:

Javascript, ES6,