JavaScript Error Invalid Assignment Left Hand Side

Last updated on by Digamber

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!

Avatar
Digamber

Digamber Rawat is from Uttarakhand, located in northwestern part of India. He is a Full-stack developer by profession and a primary author of this blog.