Solutions For The Error “TypeError: Assignment To Constant Variable” In JavaScript

TypeError: Assignment to Constant Variable in JavaScript

Don’t be confused when your program outputs the error line “TypeError: Assignment to the constant variable” in JavaScript because this error is not difficult to fix. In this article, I will give you some examples to help you understand why it occurs and then come up with the most reasonable way to fix it.

Why is the “TypeError: Assignment to constant variable” in JavaScript happening?

The “TypeError: Assignment to constant variable” in JavaScript occurs when you try to change the value of a variable declared with the keyword ‘const’ however, with an array or an object, you can change the elements of the parts for arrays and properties for objects when they are declared with the ‘const’ keyword.

The error message occurs as follows:

Uncaught TypeError: Assignment to constant variable

Example:

// For a standard variable
const str = "LearnShareIT";
str = "Hello LearnShareIT"; // Error

// For the variable is an array
const arr = [1, 2, 9, 2];
arr = [5, 7, 3]; // Error

// For the variable is an object
const obj = { property: "value" };
obj = { property: "value1" }; // Error

How to fix the “TypeError: Assignment to constant variable” in JavaScript?

For a standard variable

For a standard variable, we only have one way is that instead of using the keyword ‘const’, we can use the keyword ‘var’ or ‘let’ to declare it because if we want to change the value of the variable, we cannot use it. ‘const’ this will throw an error.

Example:

// For a normal variable
var str = "LearnShareIT";
str = "Hello LearnShareIT";

Your program will not get an error in this case because when you declare it with the keyword ‘var’ or you can declare a variable with the keyword ‘let’, the program will not throw this error.

Example:

// For a normal variable
let str = "LearnShareIT";
str = "Hello LearnShareIT";

For the variable is an array

For the variable is an array, you can use the wrong way to change the elements of the array. You cannot use an array variable created from ‘const’ to assign it to another array, but you can change the values of its elements by calling each element to change them.

Example:

// For the variable is an array
const arr = [1, 2, 9, 2];
arr[0] = 3;
arr[1] = 4;
arr[2] = 8;
arr[3] = 7;
arr = [5, 7, 3]; // Error

We cannot assign a declared array to another array. This will throw an error in your program so remember though you cannot assign to another array, you can change the value of its element, and don’t let your program throw this error again.

For the variable is an object

If we try to assign another object to an instance variable declared with ‘const’ but its properties are different, we can assign a different value to it without any error appearing.

Example:

// For the variable is an object
const obj = { property: "value" };
obj.property = "value1";
obj = { property: "value1" }; // Error

If we want our object to be assignable to another object, we can declare it with the ‘var’ or ‘let’ keyword then you can assign it to another object.

Summary

Through this article, please note that for variables declared with the keyword ‘const’, you cannot assign it with a similar value. We can only change the elements or properties of it in case the variable is an array or an object. Also, suppose the variable you create you want to assign or change its value. In that case, you should declare it with ‘var’ or ‘let’ to avoid encountering the correct “TypeError: Assignment to constant variable” in JavaScript. This article can help you, and your program. Wish you success!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *