• Skip to main content
  • Skip to search
  • Skip to select language
  • Get MDN Plus
  • English (US)

SyntaxError: invalid assignment left-hand side

The JavaScript exception "invalid assignment left-hand side" occurs when there was an unexpected assignment somewhere. It may be triggered when a single = sign was used instead of == or === .

SyntaxError or ReferenceError , depending on the syntax.

What went wrong?

There was an unexpected assignment somewhere. This might be due to a mismatch of an assignment operator and an equality operator , for example. While a single = sign assigns a value to a variable, the == or === operators compare a value.

Typical invalid assignments

In the if statement, you want to use an equality operator ( === ), and for the string concatenation, the plus ( + ) operator is needed.

Assignments producing ReferenceErrors

Invalid assignments don't always produce syntax errors. Sometimes the syntax is almost correct, but at runtime, the left hand side expression evaluates to a value instead of a reference , so the assignment is still invalid. Such errors occur later in execution, when the line is actually executed.

Function calls, new calls, super() , and this are all values instead of references. If you want to use them on the left hand side, the assignment target needs to be a property of their produced values instead.

Note: In Firefox and Safari, the first example produces a ReferenceError in non-strict mode, and a SyntaxError in strict mode . Chrome throws a runtime ReferenceError for both strict and non-strict modes.

Using optional chaining as assignment target

Optional chaining is not a valid target of assignment.

Instead, you have to first guard the nullish case.

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Why I get "Invalid left-hand side in assignment"?

There is code:

I get this error:

Why "&&" is wrong?

Vala Khosravi's user avatar

2 Answers 2

The problem is that the assignment operator, = , is a low-precedence operator, so it's being interpreted in a way you don't expect. If you put that last expression in parentheses, it works:

Pointy's user avatar

There seems to be a typo in your code:

result[id] = list[id] should be result[id] == list[id] or result[id] === list[id] (if you're doing a strict comparison)

m-ketan's user avatar

Your Answer

Sign up or log in, post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged javascript angularjs ecmascript-6 or ask your own question .

Hot Network Questions

uncaught syntaxerror invalid assignment left hand side

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

LearnShareIT

How to solve “Invalid left-hand side in assignment” in JavaScript

"Invalid left-hand side in assignment" in JavaScript

The “Invalid left-hand side in assignment” in JavaScript is a syntax error that often occurs in comparing values, such as using the “=” sign to compare. This article will give examples of everyday situations and how to fix them.

Table of Contents

What causes the “Invalid left-hand side in assignment” in JavaScript?

This is a very common syntax error. There are many causes of errors, such as wrong comparison signs, using “=” to compare two values, and not creating a variable to receive a value from the function. Here are examples of some errors.

Using the wrong comparison sign

“Invalid left-hand side in assignment” is an error caused by misspelled operator when comparing two values.

Using “=” to compare two values

This error also happens because instead of using “===”, you use “=” to compare.

Not creating a variable to receive a value from the function

This case leaves the wrong position of the variable.

Do not use square brackets when accessing object properties

For properties like this, we need to use square brackets.

Solution for the error “Invalid left-hand side in assignment” in JavaScript

Use the correct operator.

We need to pay attention to the comparison signs in expressions.

Pay attention to the position on either side of the “=”

To get the value of a function, we need to create a variable to the left of the “=” sign.

Use square brackets when accessing properties

For properties of objects with two or more words, we use square brackets.

The article has given some examples that lead to the error “Invalid left-hand side in assignment” in JavaScript. These are just syntax errors , pay attention to the process of writing code, and we will avoid such errors. We hope you can fix it quickly through this article. Good luck to you!

Maybe you are interested :

uncaught syntaxerror invalid assignment left hand side

Carolyn Hise has three years of software development expertise. Strong familiarity with the following languages is required: Python, Typescript/Nodejs, .Net, Java, C++, and a strong foundation in Object-oriented programming (OOP).

Related Posts

uncaught syntaxerror invalid assignment left hand side

Check If An Element Contains Specific Text Using JavaScript

You can use the String.includes() method and the textContent property to check if an element […]

How To Solve The Error: “ModuleNotFoundError: No module named ‘google.protobuf'” in Python

The Error: “ModuleNotFoundError: No module named ‘google.protobuf’” in Python occurs because you have not installed […]

How To Solve The Error: “ModuleNotFoundError: No module named ‘crispy_forms'” in Python

The Error: “ModuleNotFoundError: No module named ‘crispy_forms’” in Python happens because you have not installed […]

Leave a Reply Cancel reply

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

Save my name, email, and website in this browser for the next time I comment.

Invalid left-hand side in assignment in JavaScript [Solved]

avatar

Borislav Hadzhiev

Reading time · 2 min

banner

Photo from Unsplash

Invalid left-hand side in assignment in JavaScript [Solved] #

The "Invalid left-hand side in assignment" error occurs when we have a syntax error in our JavaScript code.

The most common cause is using a single equal sign instead of double or triple equals in a conditional statement.

To resolve the issue, make sure to correct any syntax errors in your code.

invalid left hand side in assignment error

Here are some examples of how the error occurs.

Use double or triple equals when comparing values #

The most common cause of the error is using a single equal sign = instead of double or triple equals when comparing values.

The engine interprets the single equal sign as an assignment and not as a comparison operator.

We use a single equals sign when assigning a value to a variable.

However, we use double equals (==) or triple equals (===) when comparing values.

Use bracket notation for object properties that contain hyphens #

Another common cause of the error is trying to set an object property that contains a hyphen using dot notation.

You should use bracket [] notation instead, e.g. obj['key'] = 'value' .

Assigning the result of calling a function to a value #

The error also occurs when trying to assign the result of a function invocation to a value as shown in the last example.

If you aren't sure where to start debugging, open the console in your browser or the terminal in your Node.js application and look at which line the error occurred.

The screenshot above shows that the error occurred in the index.js file on line 25 .

You can hover over the squiggly red line to get additional information on why the error was thrown.

book cover

Web Developer

buy me a coffee

Copyright © 2023 Borislav Hadzhiev

Related Articles

JavaScript ReferenceError – Invalid assignment left-hand side

This JavaScript exception invalid assignment left-hand side occurs if there is a wrong assignment somewhere in code. A single “=” sign instead of “==” or “===” is an Invalid assignment.

Error Type:

Cause of the error: There may be a misunderstanding between the assignment operator and a comparison operator.

Basic Example of ReferenceError – Invalid assignment left-hand side, run the code and check the console

Example 1: In this example, “=” operator is misused as “==”, So the error occurred.

Example 2: In this example, the + operator is used with the declaration, So the error has not occurred.

Output: 

Please Login to comment...

Improve your Coding Skills with Practice

Start your coding journey now.

IMAGES

  1. CRA to Vite Migration Issue (Uncaught syntaxError: Invalid left-hand side in assignment

    uncaught syntaxerror invalid assignment left hand side

  2. javascript

    uncaught syntaxerror invalid assignment left hand side

  3. Uncaught SyntaxError: Unexpected token

    uncaught syntaxerror invalid assignment left hand side

  4. Uncaught SyntaxError: Unexpected token

    uncaught syntaxerror invalid assignment left hand side

  5. Nginx 拋Uncaught ReferenceError: Invalid left-hand side in assignment 及 Uncaught SyntaxError的解決

    uncaught syntaxerror invalid assignment left hand side

  6. Nginx 拋Uncaught ReferenceError: Invalid left-hand side in assignment 及 Uncaught SyntaxError的解決

    uncaught syntaxerror invalid assignment left hand side

VIDEO

  1. Colourful Gym Chalk Crush / Dyed Blocks / Holi Powder Bombs / Powdery Reforms

  2. User defined Exceptions

  3. MTH001 ASSIGNMENT NO 1 CORRECT SOLUTION |valid and invalid argument|Toutology|S KHAN ACADEMY||

  4. 2. Creating object of exception class

  5. Programming In Modern C++ week 8 W8_programming_Qs 1 2 3 programming assignment solution 1 2

  6. How to Debug, Understand and Fix Syntax Errors

COMMENTS

  1. SyntaxError: invalid assignment left-hand side

    Invalid assignments don't always produce syntax errors. Sometimes the syntax is almost correct, but at runtime, the left hand side expression evaluates to a value instead of a reference, so the assignment is still invalid. Such errors occur later in execution, when the line is actually executed.

  2. Why I get "Invalid left-hand side in assignment"?

    The problem is that the assignment operator, =, is a low-precedence operator, so it's being interpreted in a way you don't expect. If you put that last expression in parentheses, it works: for (let id in list) ( (!q.id || (id == q.id)) && (!q.name || (list [id].name.search (q.name) > -1)) && (result [id] = list [id]) ); Share

  3. How to solve "Invalid left-hand side in assignment" in

    Uncaught SyntaxError: Invalid left-hand side in assignment Using “=” to compare two values This error also happens because instead of using “===”, you use “=” to compare. Example: 4 1 2 if (10 = 10) { 3 console.log("test"); 4 } Output: Uncaught SyntaxError: Invalid left-hand side in assignment

  4. Invalid left-hand side in assignment in JavaScript [Solved]

    The "Invalid left-hand side in assignment" error occurs when we have a syntax error in our JavaScript code. The most common cause is using a single equal sign instead of double or triple equals in a conditional statement. To resolve the issue, make sure to correct any syntax errors in your code.

  5. JavaScript ReferenceError

    ReferenceError: invalid assignment left-hand side Error Type: ReferenceError Cause of the error: There may be a misunderstanding between the assignment operator and a comparison operator. Basic Example of ReferenceError – Invalid assignment left-hand side, run the code and check the console Example 1: Javascript if (Math.PI = 10 || Math.PI = 5) {