JavaScript let: Mutable Block-Scoped Bindings
Quick answer
Use let when a local variable must receive a new value later. A declaration creates the binding; an assignment changes its current value. The assigned value does not have to retain the original type.
How it works
Write let name = initialValue; and later name = nextValue;. A declaration without an initializer, such as let total;, initializes the binding to undefined when that statement executes. The name is local to its enclosing block or other lexical scope. You cannot declare the same lexical name twice in one scope.
let seats = 12;seats = seats - 3;console.log(seats);Expected output in Node.js:
9The expression reads the old value before assignment.
let total;console.log(total);total = 6 * 4;console.log(total);Expected output in Node.js:
undefined24The first line has executed before the first read.
Common mistakes
Do not write let seats again just to update seats. Use assignment without the declaration keyword. Reading the name before its declaration executes is different from reading a declared binding whose value is undefined.
Practice
In the exercise, track a changing score through two adjustments and return the final value.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript let: Mutable Block-Scoped Bindings — Knowledge Check
Apply the taught concept and reason about the result.
What does let balance = 10; balance = balance - 4; console.log(balance); print?
What is the value of an executed let pending; declaration?
Which statement updates an existing let points binding?
Can let result = 3; result = "done"; run?
When should a let declaration be preferred over const?