Sometimes, we may run into the ‘TypeError: invalid assignment to const "x"’ when we’re developing JavaScript apps.
In this article, we’ll look at how to fix the ‘TypeError: invalid assignment to const "x"’ when we’re developing JavaScript apps.
Fix the ‘TypeError: invalid assignment to const "x"’ When Developing JavaScript Apps
To fix the ‘TypeError: invalid assignment to const "x"’ when we’re developing JavaScript apps, we should make sure we aren’t assigning a variable declared with const
to a new value.
On Firefox, the error message for this error is TypeError: invalid assignment to const "x"
.
On Chrome, the error message for this error is TypeError: Assignment to constant variable.
And on Edge, the error message for this error is TypeError: Assignment to const
.
For example, the following code will throw this error:
const COLUMNS = 80;
// ...
COLUMNS = 100;
We tried to assign 100 to COLUMNS
which is declared with const
, so we’ll get this error.
To fix this, we write:
const COLUMNS = 80;
const WIDER_COLUMNS = 100;
to declare 2 variables, or we can use let
to declare a variable that we can reassign a value to:
let COLUMNS = 80;
// ...
COLUMNS = 100;
Conclusion
To fix the ‘TypeError: invalid assignment to const "x"’ when we’re developing JavaScript apps, we should make sure we aren’t assigning a variable declared with const
to a new value.
On Firefox, the error message for this error is TypeError: invalid assignment to const "x"
.
On Chrome, the error message for this error is TypeError: Assignment to constant variable.
And on Edge, the error message for this error is TypeError: Assignment to const
.