References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript let statement

Statement JavaScript All modern browsers Updated
Quick answer

The let keyword declares a block-scoped variable you can reassign later. let count = 0; count = 1;. It's block-scoped (limited to the nearest { }), which makes it safer than the old var. Use const by default and let only when the value needs to change.

Overview

let declares a variable whose value you intend to change. It's one of the two modern ways to declare variables (alongside const), and it replaced the older var in everyday code for good reasons.

The big one is block scope. A let variable exists only inside the nearest curly braces — an if block, a loop body, a function. That's far more predictable than var, which is function-scoped and leaks out of blocks. Block scope is exactly why let is the right counter for a for loop: each iteration gets its own copy, which fixes a classic class of closure bugs.

The common guidance is simple: default to const, and use let only when you genuinely need to reassign. That makes "this value changes" visible at a glance. Note that let variables can't be used before their declaration line (the "temporal dead zone"), which catches mistakes early, and can't be redeclared in the same scope.

Syntax

let count = 0;
count = count + 1;  // reassignment is allowed

{
  let scoped = "only in here";
}
// scoped is not accessible here

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  let score = 0;
  score += 10;
  score += 5;

  document.getElementById('out').textContent = 'Score: ' + score; // Score: 15
</script>

Best practices

  • Default to const; use let only when the variable must be reassigned.
  • Prefer let over var always — block scope is safer and clearer.
  • Declare variables close to where they're first used, not at the top out of habit.
  • Use let for loop counters so each iteration is correctly scoped.

Frequently asked questions

What is the difference between let and const?
Both are block-scoped, but a let variable can be reassigned and a const cannot. Use const by default and let when the value needs to change.
What is the difference between let and var?
let is block-scoped and can't be used before its declaration; var is function-scoped and hoisted, which causes subtle bugs. Prefer let.
What is block scope?
A let variable only exists inside the nearest { } — a loop, an if, a function — and isn't visible outside it.
What is the temporal dead zone?
The region from the start of a block to a let/const declaration where the variable can't be accessed yet. Using it there throws a ReferenceError.