Categories
JavaScript Answers

How to Fix the ‘ReferenceError: invalid assignment left-hand side’ Error in Our JavaScript App?

Spread the love

Sometimes, we may run into the ‘ReferenceError: invalid assignment left-hand side’ when we’re developing JavaScript apps.

In this article, we’ll look at how to fix the ‘ReferenceError: invalid assignment left-hand side’ when we’re developing JavaScript apps.

Fix the ‘ReferenceError: invalid assignment left-hand side’ When Developing JavaScript Apps

To fix the ‘ReferenceError: invalid assignment left-hand side’ when we’re developing JavaScript apps, we should make sure that we’re accidentally using the = operator for doing comparisons.

Also, we should make sure our assignment statements have variables on the left side to assign to.

For instance, we’ll get this error is we write something like:

if (Math.PI = 3 || Math.PI = 4) {
  console.log('error');
}

Also, we should make sure that we have a variable on the left side of the assignment statement.

Therefore, we shouldn’t write statements like:

let  str = 'Hello, '
+= 'is it what '
+= 'you\'re looking for?';

To fix the first piece of code, we write:

if (Math.PI === 3 || Math.PI === 4) {
  console.log('error');
}

And to fix the 2nd piece of code, we write:

let str = "Hello, ";
str += "is it what ";
str += "you're looking for?";

Conclusion

To fix the ‘ReferenceError: invalid assignment left-hand side’ when we’re developing JavaScript apps, we should make sure that we’re accidentally using the = operator for doing comparisons.

Also, we should make sure our assignment statements have variables on the left side to assign to.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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