let Statement
EasyIn plain terms
let declares a block-scoped variable. Unlike var, it is only visible inside the block (curly braces) where it is declared. This prevents accidental reassignment and scope leaks.
What you need to know
- •Block-scoped
- •Temporal Dead Zone until declaration
- •Recommended for mutable variables
Try it yourself
Copy the code below and run it in your browser console or a code editor:
{
let x = 10;
console.log(x); // 10
}
// console.log(x); // Error: x is not defined
for (let i = 0; i < 3; i++) {
console.log(i); // 0, 1, 2 - each iteration has its own i
}Learn more
Dive deeper with these trusted resources: