Skip to content
engineering workbench.by StudyPort Academy
All errors
runtime · DEBUG LIBRARY

Assignment to constant variable

Locate the failed assumption, reproduce it, then repair the cause.

What actually caused it

A const was assigned another value after initialization. The error normally appears with a TypeError prefix. Increment operators and compound assignments also reassign the binding, so count++ can trigger it.

Const protects the , not the contents of an or . You can change an object's property or push into a const array because the binding still refers to the same object. Assigning a replacement object or array is different.

Changing every const to let may hide a mistaken reassignment. First decide whether this is meant to evolve or whether the code should produce a separate value.

Minimal reproduction

A counter that reassigns const

const count = 1;
count += 1;

Replacing a const object

const settings = { theme: "light" };
settings = { theme: "dark" };

The fix

Use let when the same must receive a new value over time. Keep const for stable bindings, and use a new name when you are creating a distinct result.

If the intended operation changes contents, mutate deliberately or create a replacement under another according to your state-management rules. Const does not supply deep ; freezing and immutable-update strategies are separate decisions.

Allow intentional reassignment

let count = 1;
count += 1;
console.log(count);

Keep the binding, change its property

const settings = { theme: "light" };
settings.theme = "dark";
console.log(settings.theme);

This is , which may or may not suit the surrounding state design.