01 / 20

let & const

Learn how to declare variables in modern JavaScript using the two keywords that replaced var: choosing the right one makes your code safer and easier to read.

JavaScript

What Is a Variable?

A variable is a named container that stores a value in your program's memory. Think of it as a labelled box: the label is the variable's name, and what you put inside is its value.

score 95 name "Ada" PI 3.14 label = name    |    contents = value

Declaring with let

Use let when you need a variable whose value will change later in your program. You can reassign it as many times as you like.

let score = 0;
score = score + 10; // now 10 ✓
score = 25; // now 25 ✓

Declaring with const

Use const when a variable should never be reassigned after its initial value. The engine will throw an error if you try.

const TAX_RATE = 0.08;
const greeting = "Hello!";

TAX_RATE = 0.10; // TypeError! ✕

The Golden Rule

Will this value be reassigned? YES NO let const

Block Scope Explained

Both let and const are block-scoped: they only exist inside the nearest pair of curly braces { } where they are declared.

Global / outer block let a = 1; if block let b = 2; ✓ b lives here console.log(a); ✓ can see outer a console.log(b); ✕ ReferenceError: b is not defined

let vs const vs var

let
Block-scoped. Can be reassigned. Cannot be redeclared in the same scope.
const
Block-scoped. Cannot be reassigned. Must be initialized at declaration.
var
Function-scoped. Can be reassigned and redeclared. Hoisted with value undefined.

Hoisting & the Temporal Dead Zone

Variables declared with let and const are hoisted to the top of their block, but they are not initialized. Accessing them before the declaration line causes a ReferenceError. This gap is called the Temporal Dead Zone (TDZ).

TEMPORAL DEAD ZONE console.log(x); ✕ ReferenceError x = 5; ✕ ReferenceError DECLARATION LINE let x = 10; ✓ Initialized console.log(x); ✓ Outputs 10

How to Choose the Right Keyword

1
Start with const. Every new variable should be const by default.
2
Ask: will I reassign this? If you need to update the binding (a counter, a toggle, a running total), switch to let.
3
Declare as close to first use as possible. Keep the variable near the code that reads it.
4
Never use var. If you see it in old code, refactor to let or const.

Example 1: Counting Loop

const fruits = ["apple", "banana", "cherry"];
let count = 0;

for (let i = 0; i < fruits.length; i++) {
  count += 1;
}

console.log(count); // 3

Example 2: const with Objects

const user = { name: "Ada", age: 17 };

user.age = 18; // ✓ mutating a property
user.email = "ada@x.co"; // ✓ adding a property

user = { name: "Bob" }; // ✕ TypeError!

Example 3: Real-World Pattern

const API_URL = "https://api.example.com";
const MAX_RETRIES = 3;

let attempts = 0;
let success = false;

while (!success && attempts < MAX_RETRIES) {
  attempts += 1;
  success = tryFetch(API_URL);
}

const ≠ Immutable

const colors = "red" "blue" "green" X colors = ["yellow"]; ✕ Cannot reassign the binding colors.push("yellow"); ✓ Mutating contents is allowed "red" "blue" "green" "yellow" const protects the binding, not the value itself

Common Mistakes

✕ Using var by habit
Old tutorials still show var. Always translate to let or const when you follow along.
✕ Using let for everything
If a value never changes, const is better. It signals intent and prevents accidental reassignment.
✕ Thinking const = immutable
Objects and arrays declared with const can still have their properties or elements changed.

Summary Comparison

Feature let const var
Scope Block Block Function
Reassignable Yes No Yes
Redeclarable No No Yes
Hoisted TDZ TDZ undefined
Must initialize No Yes No

Question 1 of 5

Which keyword should you use for a variable that never needs to be reassigned?

Question 2 of 5

What happens when you run this code?

const x = 10; x = 20;

Question 3 of 5

const arr = [1, 2, 3]; arr.push(4);

Does this code throw an error?

Question 4 of 5

What is the scope of variables declared with let and const?

Question 5 of 5

What does the Temporal Dead Zone (TDZ) refer to?