The Invalid Assignment Left-Hand Side Error Explained
The name of this error indicates that this error is caused by a faulty or buggy assignment statement. The “left-hand side error” part of this error means that the value at the left-hand side of the assignment operator was not a value that could have been set equal to something using the assignment operator.
However, this error is not caused due to the assignment operator, and it is actually caused by the misuse of the assignment operator where the actual intention was to use the “==” or the “===” operator.
Creating the ReferenceError – Invalid Assignment Left-Hand Side Error
To create the error in focus using the following lines:
Executing the program will show an error in the terminal. Take a look at the error message of the “ReferenceError – Invalid Assignment Left-Hand Side”:
It is pretty clear from the above image that the error message doesn’t really help the user that much apart from highlighting the line that has caused the error.
Debugging the ReferenceError – Invalid Assignment Left-Hand Side Error
To debug the error take a look at the code again:
In this code snippet:
The user is trying to add a value to Math.PI, which is actually a constant, meaning that its value cannot be changed not by using the “+” operator or even by the assignment operator.
Now, to fix this error, one must be clear about what to do or what was the real intention of the statement. If the task was to compare the values, then simply changing the “=” operator to “==” will fix the error. But if the assignment was the actual task, then simply change the constant in focus to a variable by using a keyword var.
In the above, the solution is to change the assignment operator to an equality “==” operator like this:
If the program is executed now, it will produce the following result:
The program didn’t crash and the result was printed on the terminal