JavaScript Error Invalid Assignment Left Hand Side

By Digamber Rawat Last updated 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
Digamber Rawat

Full stack developer with a passion for UI/UX design. I create beautiful and useful digital products to solve people’s problem and make their life easy.

Hire Me