</>
Skip to content
JavaScript lessons (9/64)

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

letconstvar
Reassign?YesNeverYes
ScopeBlockBlockFunction
Use todayWhen it changesDefaultAvoid

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 silent undefined.

Mini Practice

  1. Reassign a let three times; log between hops
  2. Prove block scope: declare inside an if, read outside, catch the error
  3. Try redeclaring; read the exact error text
  4. Convert a var loop to let and 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.