let
Declares a variable whose value can change.
Available since: ES2015
Syntax
let name = valueExamples
A changeable variable:
let score = 0;
score = score + 10;
console.log(score);Output:
10The variable exists only inside its block:
let message = 'outer';
if (true) {
let message = 'inner';
console.log(message);
}
console.log(message);Output:
inner
outerNotes
The scope of let is the block in curly braces. You cannot declare the same variable twice in one block, and using it before the declaration throws a ReferenceError.
See also
- const — Declares a variable that cannot be reassigned.
- var — The legacy way to declare a variable.
- ReferenceError — A variable does not exist error.
In the tutorial
- Variables — How to declare variables in JavaScript with let and const, name them and insert them into strings.