ShareYourGroupShareYourGroupError Explainer

JavaScript TypeError: Assignment to constant variable

A plain-words explanation of this JavaScript message: what it means, the usual causes, and the steps that fix it.

The messageUncaught TypeError: Assignment to constant variable.

Make the error yourself

Run in Google Chrome 157.0.8103.51. This is the smallest example we could find that shows it.

JavaScript (in a script tag)

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

Browser console (the real message)

TypeError: Assignment to constant variable.

Why it happens

A variable declared with const cannot be given a new value after its first assignment. The error appears the moment the program tries count = ... or count++ on it. Note that const stops reassigning only, not changing the inside of an object or array: const list = []; list.push(1) is fine, but list = [] again is not.

How to fix it

  1. If the value must change, declare it with let.
  2. If it must not change, find the second assignment and give the new value another name.
  3. For objects and arrays, change their contents (list.push(x), user.name = "A") instead of replacing the whole variable.

The corrected version

JavaScript (in a script tag)

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

Browser console (the corrected version)

2

How to avoid it

Use const by default and let only when you know that a value will change. That way an accidental change shows up as an error.

Questions

Can I change a property of a const object?

Yes. const user = {}; user.name = "A"; works. Only user = {...} fails.

This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.