Solution to the problem of “TypeError: Assignment to constant variable”

In the process of project development, if you are not careful when using variable declarations, you may cause type errors
such as:

Uncaught (in promise) TypeError: Assignment to constant variable. Uncaught (in promise) TypeError: Assignment to constant variable
.

Reason:
We use const to define the variable and there is an initial value. Assign a value to this variable later, so an error was reported.

The ES6 standard introduces a new keyword const to define constants. Both const and let have block-level scope:

The value of a constant defined by const cannot be modified, and the defined constant must be assigned an initial value;
let defines a variable, which can be assigned to a variable without initial value assignment.
This error is caused because we modified the constant. Although some browsers do not report an error, it has no effect!

Solution:
Change const to let to declare.

Similar Posts:

Leave a Reply

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