JavaScript Error Invalid Assignment Left Hand Side

Article By Digamber Rawat on

In this tutorial, I am going to talk about invalid assignment left-hand side JavaScript error. This error occurs because of the mismatch of a comparison and assignment operator.

Let us check it out why this error occurs, in the below example. As you can see we have used two numerical variables and we are checking those numbers in the JavaScript if condition. You will see our small programme will throw the Uncaught ReferenceError: Invalid left-hand side assignment error because we are not using the proper comparison operators.

let a = 4,
    b = 5;

if (a = 4 || b = 5) {
    console.log('This works!');
}

// Uncaught ReferenceError: Invalid left-hand side in assignment

Now solve this error by putting "==" instead of single "=" operator in if statement.

let a = 4,
    b = 5;

if (a == 4 || b == 5) {
    console.log('This works!');
}

// Output: This works!
Digamber Rawat

Feel free to contact me, If you are looking for a freelance full stack Developer, with the following skills: Mongo DB, Express JS, Angualr 2+, Node JS, Loopback JS, Firebase, Ionic Framework and WordPress. I also offer remote contracting service to the clients across the globe. Hire Me