How to Resolve "TypeError: Assignment to constant variable" Error in JavaScript
The TypeError: Assignment to constant variable is a common error in modern JavaScript that occurs when you try to reassign a value to a variable that was declared using the const keyword.
This guide will explain the fundamental rule of const declarations, clarify the important distinction between a constant reference and a constant value, and show you the correct way to solve this error.
The Core Concept: What const Really Means
The const keyword declares a constant reference to a value. This means two things:
- The variable must be initialized at the time of declaration.
- The variable cannot be reassigned to point to a different value or a different object in memory.
It does not mean that the value itself is immutable (unchangeable).
The Cause of the Error: Reassigning a const Variable
This error occurs when you declare a variable with const and then later try to assign a new value to it using the assignment operator (=).
Example of problem:
// Problem: The variable `PI` is declared as a constant.
const PI = 3.14159;
// ⛔️ TypeError: Assignment to constant variable.
PI = 3.14; // You cannot reassign a `const` variable.
The same error occurs if you try to re-declare it:
const name = 'Alice';
// ⛔️ SyntaxError: Identifier 'name' has already been declared
const name = 'Bob';
const is designed to prevent these kinds of reassignments, which helps to avoid bugs.
The Solution: Use let for Variables That Need to Change
The fix for this error is to choose the correct variable declaration keyword based on your intent.
- Use
constfor values that should never be reassigned (e.g., mathematical constants, imported modules, DOM elements you only select once). - Use
letfor values that you expect to change over time (e.g., counters, loop variables, or any variable that will be reassigned).
Solution:
// ✅ Correct: Use `let` because the value of `score` will change.
let score = 100;
console.log(score); // Output: 100
// Now, reassigning the variable is allowed.
score = score + 10;
console.log(score); // Output: 110
The "Object and Array" Misconception: const Does Not Make Values Immutable
This is a critical and often misunderstood concept. When you declare an object or an array with const, it means the reference to that object/array is constant. You cannot assign a completely new object/array to the variable.
However, you can still change the internal contents of that object or array.
Example:
// ✅ This is valid. The `user` variable always points to the SAME object.
const user = {
name: 'Alice',
id: 1,
};
// You can change the properties of the object.
user.name = 'Bob'; // This is allowed.
user.isOnline = true; // This is allowed.
console.log(user); // Output: { name: 'Bob', id: 1, isOnline: true }
// ⛔️ But you CANNOT reassign the variable to a new object.
// user = { name: 'Charlie', id: 2 }; // TypeError: Assignment to constant variable.
The same principle applies to arrays:
// ✅ This is valid. The `numbers` variable always points to the SAME array.
const numbers = [1, 2, 3];
numbers.push(4); // This is allowed (mutates the array's content).
numbers[0] = 99; // This is also allowed.
console.log(numbers); // Output: [99, 2, 3, 4]
// ⛔️ But you CANNOT reassign the variable to a new array.
// numbers = [5, 6, 7]; // TypeError: Assignment to constant variable.
Conclusion
The TypeError: Assignment to constant variable is a feature, not a bug. It's JavaScript's way of enforcing the rule that const variables cannot be reassigned.
- To solve the error, use
letinstead ofconstif you know the variable's value needs to change. - Use
constas your default for all variables to prevent accidental reassignments. - Remember that
constdoes not make objects or arrays immutable; it only protects the variable's reference to them. You can still modify the contents of aconstobject or array.