Values and variables
Let vs Const in JavaScript
Should you use let or const in JavaScript? My take: default to const because it prevents reassignment, and only reach for let when you truly need to reassign.
In JavaScript, we declare variables with let and const.
When should you pick one over the other?
I always default to const.
Why?
Because const guarantees the binding cannot be reassigned.
When I program, I want the construct that can hurt me the least. We already juggle enough state, async timing, and typos.
The more power you give a variable, the more ways it can surprise you later.
If I declare with let, I allow reassignment:
let number = 0
number = 1
Sometimes that is exactly what you need. Counters, loop indices, and flags that flip are real cases.
If I declare with const, reassignment is a syntax error:
const number = 0
number = 1 // TypeError: Assignment to constant variable
That error is useful. It tells me I tried to reuse a name for a new value when I did not mean to.
Roughly four out of five variables in my code never need reassignment. For those, const documents intent: this name always points at the same value.
Switch to let when you genuinely need to reassign. A loop counter is the classic example:
let total = 0
for (const price of [10, 20, 30]) {
total = total + price
}
console.log(total) // 60
Here total must change, so let is correct.
Remember: const only locks the binding, not the object contents. You can still push to a const array or change properties on a const object.
Some teams use a linter rule that flags every let without a reassignment. That sounds strict, but it matches how I already think about variables.
The habit also helps in loops. When the loop variable itself must change, let is correct. When you only collect results into another binding, keep the accumulator on const and build a new array with push or spread.
Try this on your own code: search for every let and ask whether that name is ever reassigned. If not, change it to const and run your tests.
Lesson completed