Assignment to constant variable
JavaScript Programming Language
Severity: MinorWhat Does This Error Mean?
This error means you tried to change the value of a variable declared with 'const'. Const means constant — once you set it, you cannot reassign it to a new value. For example: const score = 10; score = 20; — the second line crashes because score is a constant. The fix is to use 'let' instead of 'const' if you need to change the variable's value.
Affected Models
- All browsers
- Node.js
- ES6 and later
Common Causes
- Declaring a variable with const and then trying to reassign it to a new value
- Using a for loop to increment a const counter — like: const i = 0; i++
- A function parameter was accidentally named the same as a const variable in the outer scope
- Misunderstanding that const with an object or array still lets you change properties — but not reassign the whole variable
- Copying code that used let and changing the declaration to const without realizing the variable gets reassigned later
How to Fix It
-
Find the variable declaration and change 'const' to 'let' if the variable needs to change after being set.
Before: const score = 10; After: let score = 10; — now score = 20; works fine.
-
Ask yourself if the variable should really change. If the value should always stay the same (like a maximum size or a fixed URL), keep const and fix the code that tries to change it.
Const is a good habit for values that should never change — it prevents accidental reassignment and makes your code easier to reason about.
-
In for loops, use let for the loop counter, not const: for (let i = 0; i < 10; i++) — never for (const i = 0; i < 10; i++)
The loop counter changes on every iteration, so it must be let. However, for...of loops can use const for the loop variable since each iteration creates a new binding: for (const item of myArray)
-
Remember that const prevents reassignment but not mutation. You CAN change properties of a const object or push items to a const array.
const person = {name: 'Alice'}; person.name = 'Bob'; — this works. person = {name: 'Bob'}; — this crashes. The object can change; the variable cannot.
-
If you want to prevent an object from being changed at all (not just reassigned), use Object.freeze(): const config = Object.freeze({maxItems: 10});
Object.freeze() prevents adding, removing, or changing properties. It does not work deeply on nested objects though — only the top level is frozen.
When to Call a Professional
This is a straightforward error to fix — change const to let if the variable needs to be reassigned. The bigger question is whether the variable should change at all. If it should always stay the same, keep const and fix the logic.
Frequently Asked Questions
What is the difference between const, let, and var?
const declares a variable that cannot be reassigned — it stays the same value. let declares a variable that can be reassigned — you can change it later. var is the old way — it works like let but has different scoping rules and is best avoided in modern code. Use const by default, and switch to let only when you know the variable will change.
Can I change properties of a const object?
Yes — const only prevents you from reassigning the variable to a completely different object. You can still change, add, or remove properties of the object itself. Example: const user = {name: 'Alice'}; user.name = 'Bob'; — this works fine. The variable user still points to the same object, you just changed something inside it.
Should I use const or let by default?
Use const by default for everything. Only switch to let when you actually need to reassign the variable. This habit prevents bugs — if you later accidentally try to reassign a const variable, you get an error that helps you catch the mistake. Most modern JavaScript style guides recommend const-first.