"Assignment to constant variable" in JavaScript — What It Means and How to Fix It
What the JavaScript error "Assignment to constant variable" means and how to fix it.
Published October 30, 2026
You tried to reassign a variable declared with const. Constants cannot be rebound, though objects they hold can still be mutated.
What it means and why it happens
Typical causes: reassigning in loops or accidental reuse of a name.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
TypeError: Assignment to constant variable.
Practical examples
Code that triggers it
const total = 0
total = 5The fix
let total = 0
total = 5
// object contents may change
const user = { n: 1 }; user.n = 2Freeze if needed
const cfg = Object.freeze({ a: 1 })Common mistakes
- Using const for values that change.
- Thinking const makes objects immutable.
- Reassigning loop variables.
Key takeaways
- TypeError: Assignment to constant variable: You tried to reassign a variable declared with const.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
const vs Object.freeze?
const prevents rebinding; freeze prevents mutating properties.
How do I find which line caused TypeError: Assignment to constant variable?
The error message and stack trace show the file and line; start there and work backwards.