When to Use let vs const
What you'll learn: You'll learn simple rules to help you decide whether to use let or const when declaring variables.
The Decision Framework
Now that you know let allows reassignment and const prevents it, how do you choose between them in real code?
The golden rule is: ask yourself if the variable will ever be reassigned a new value. If the answer is "no," use const. If "yes," use let.
When to Use const
Use const as your default choice. Most variables in well-written JavaScript don't actually need to be reassigned:
const userName = 'Alice';
const maxLoginAttempts = 3;
const colors = ['red', 'green', 'blue'];
Remember: const still lets you modify objects and arrays—it just prevents reassigning the variable itself. This is usually what you want.
When to Use let
Use let only when you know the variable will be reassigned:
let score = 0;
score = score + 10; // Reassignment needed
let currentPage = 1;
currentPage = 2; // Will change later
Counter variables, accumulating values, and state that changes over time—these are good candidates for let.
Think of It Like This
Imagine const as a locked box with a clear window: you can see inside and change the contents, but you can't swap the entire box for a different one. let is an unlocked box you can completely replace.
Key Takeaway: Default to const for all variables. Only use let when you're certain you'll need to reassign the variable to a different value. This makes your code's intent clearer and helps prevent accidental reassignment bugs.