JavaScript - let Statement

Overview

Estimated time: 10–20 minutes

let declares a block-scoped, mutable binding, ideal for values that will change.

Learning Objectives

  • Use let inside blocks and loops.
  • Avoid scope leakage common with var.

Prerequisites

Block scope

if (true) {
  let x = 1;
}
// console.log(x); // ReferenceError: x is not defined

Loop bindings

for (let i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0); // 0, 1, 2
}

Common Pitfalls

  • Accessing a let binding before its declaration triggers the temporal dead zone (TDZ).