JavaScript — Let
let declares a changeable variable
let score = 0;
score = 100; // reassignment — fine
score = score + 1; // 101
Block scope — the big upgrade
Variables born inside { } stay inside:
{
let temp = "secret";
}
console.log(temp); // ReferenceError — never left the block
if (true) {
let x = 1;
console.log(x); // 1 ✓ inside is fine
}
console.log(x); // Error outside
Loops get a fresh copy each iteration — the classic var bug simply can't happen:
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i)); // 0, 1, 2 ✓
}
Rules of engagement
let user; // declared, value undefined until assigned
let a = 1, b = 2; // multiple in one line (works, less readable)
user = "Ada"; // second declaration would ERROR:
// let user = "Bo"; // SyntaxError: already declared
Re-declaring errors are a feature — typos can't silently create new variables.
let vs var vs const
let | const | var | |
|---|---|---|---|
| Reassign? | Yes | Never | Yes |
| Scope | Block | Block | Function |
| Use today | When it changes | Default | Avoid |
Decision flow: start with const; switch to let only when reassignment proves necessary; never reach for var again.
Common mistakes
Redeclaring in the same scope —
SyntaxError, not a warning.Using before declaring — ReferenceError ("temporal dead zone"), unlike
var's silentundefined.
Mini Practice
- Reassign a
letthree times; log between hops - Prove block scope: declare inside an
if, read outside, catch the error - Try redeclaring; read the exact error text
- Convert a
varloop toletand verify timeouts print 0-1-2
Next: const →
Related Topics
Frequently Asked Questions about Let
What is Let in JavaScript?
Let is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Let?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Let.
Why is Let important in JavaScript?
Let is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.