Course contentsShow
← JavaScript
Lesson 67 of 2,7233. Variables and ScopeFree lesson

Declaring Variables with let

Understand let for block-scoped variables that can be reassigned.

Declaring Variables with let

What you'll learn: How to use let to create variables that live only in their block and can be updated when needed.

What Is let?

let is the modern way to declare a variable in JavaScript when you know its value might change. Unlike var (which you just learned), let is block-scoped—meaning it only exists within the curly braces {} where it's defined.

Think of let like a name tag you put on a box. You can swap out what's inside the box (reassign the value), but the box only exists in one room of your house (one block of code).

How to Use let

let age = 25;
console.log(age); // 25

age = 26; // Reassignment is allowed
console.log(age); // 26

You declare it once with let, then you can change its value later without using let again.

Block Scope in Action

let score = 10;

if (true) {
  let score = 20; // Different variable, only exists in this block
  console.log(score); // 20
}

console.log(score); // 10 (outer variable unchanged)

The score inside the if block is completely separate from the outer score.

Why Use let Over var?

let prevents accidents. Because it's block-scoped, you won't accidentally overwrite variables from outer scopes or create confusion with function-scoped behavior.

Key Takeaway: Use let to declare variables that can be reassigned and need to stay contained within their block—it's safer and clearer than var.