• Skip to main content
  • Skip to search
  • Skip to select language
  • Sign up for free
  • Português (do Brasil)

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 statement 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.

  • Assignment operators
  • Equality operators
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand
  • OverflowAI GenAI features for Teams
  • OverflowAPI Train & fine-tune LLMs
  • Labs The future of collective knowledge sharing
  • About the company Visit the blog

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.

Get early access and see previews of new features.

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

There is code:

I get this error:

Why "&&" is wrong?

  • ecmascript-6

Vala Khosravi's user avatar

  • are you missing if before the first set of parentheses? –  Dan O Commented Dec 1, 2017 at 3:32
  • 1 result[id] = list[id] should be result[id] === list[id] as it is a condition –  Aravind Commented Dec 1, 2017 at 3:33
  • @Aravind that is not correct. –  Pointy Commented Dec 1, 2017 at 3:38
  • @DanO "if" must not be important, because all logical operator in ( ), not { } –  Turar Abu Commented Dec 1, 2017 at 3:43
  • @TurarAbu see my answer: the problem is that your assignment expression at the end of the && list is not in parentheses. –  Pointy Commented Dec 1, 2017 at 3:43

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

  • The real problem is using logical operators for control flow. –  user8897421 Commented Dec 1, 2017 at 3:50
  • 1 @rockstar well sure :) But it's not erroneous and lots of hard-core library code does stuff like that, so people will see it in "respectable" places. –  Pointy Commented Dec 1, 2017 at 3:55

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

  • No. The code posted is using JavaScript logical short-circuiting operators to conditionally perform an operation. The change you suggest would make the code do nothing at all. –  Pointy Commented Dec 1, 2017 at 3:37
  • The trouble at 4'th line. –  Turar Abu Commented Dec 1, 2017 at 3:40
  • There's no way = is a short hand for == or === . They have different meanings altogether. I checked in my console and it worked fine when I changed it to result[id] == list[id] –  m-ketan Commented Dec 1, 2017 at 3:41
  • @m-ketan he wants to use the assignment operator ( = ). If the statement does not perform an assignment, the code is useless. –  Pointy Commented Dec 1, 2017 at 3:42
  • @Pointy Oh I see it now. –  m-ketan Commented Dec 1, 2017 at 3:44

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

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 and acknowledge you have read our privacy policy .

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

  • The Overflow Blog
  • Scaling systems to manage all the metadata ABOUT the data
  • Navigating cities of code with Norris Numbers
  • Featured on Meta
  • We've made changes to our Terms of Service & Privacy Policy - July 2024
  • Bringing clarity to status tag usage on meta sites
  • Tag hover experiment wrap-up and next steps

Hot Network Questions

  • Writing a Puzzle Book: Enigmatic Puzzles
  • Why do instructions for various goods sold in EU nowadays lack pages in English?
  • Many and Many of - a subtle difference in meaning?
  • Claims of "badness" without a moral framework?
  • Shift right by half a trit
  • Suitable tool bag for vintage centre pull rim brake bike
  • Is it normal to be able to tear off PCB components with bare hands?
  • What are those bars in subway train or bus called?
  • Argument of Complex Numbers with unknown values
  • Would several years of appointment as a lecturer hurt you when you decide to go for a tenure-track position later on?
  • How much air escapes into space every day, and how long before it makes Earth air pressure too low for humans to breathe?
  • What was Jesus's relationship with God ("the father") before Jesus became a "begotten son"?
  • can a CPU once removed retain information that poses a security concern?
  • I need to better understand this clause in an independent contract agreement for Waiverability:
  • If there is no free will, doesn't that provide a framework for an ethical model?
  • Could a 3D sphere of fifths reveal more insights than the 2D circle of fifths?
  • Why didn't Walter White choose to work at Gray Matter instead of becoming a drug lord in Breaking Bad?
  • How is it decided whether a creature can survive without a head?
  • How to Vertically Join Images?
  • Specified data directory does not exist - but it does
  • Automotive Controller LDO Failures
  • Do comets ever run out of water?
  • Should I pay off my mortgage if the cash is available?
  • In "Take [action]. When you do, [effect]", is the action a cost or an instruction?

invalid assignment left hand side servicenow

How to fix SyntaxError: invalid assignment left-hand side

invalid assignment left hand side servicenow

Let me show you an example that causes this error and how I fix it.

How to reproduce this error

How to fix this error, other causes for this error.

You can also see this error when you use optional chaining as the assignment target.

Take your skills to the next level ⚡️

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

avatar

Last updated: Mar 2, 2024 Reading time · 2 min

banner

# 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.

use 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.

assignment vs equality

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.

use bracket notation for object properties containing hyphens

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

Borislav Hadzhiev

Web Developer

buy me a coffee

Copyright © 2024 Borislav Hadzhiev

Create an assignment rule

Create an assignment rule and apply it to a single table. Assignment rules are\n designed to run at the time you open a record.

  • \n Navigate to All > System Policy > Rules > Assignment and click New . \n
Table 1. Assignment rule form
FieldDescription
NameThe descriptive name for the assignment rule.
ActiveSpecifies whether the assignment rule is active. Only\n active assignment rules take effect.
Applies to
Table

The table with the records that the assignment rule\n applies to.

\n\n

The list shows only tables and database views that\n are in the same scope as the assignment rule. If you\n select a custom table that extends the task table,\n and for the assignment rule to work properly, you\n must clear the instance cache by navigating to\n https://<instance_name>.service-now.com/cache.do.

\n\n Clearing the system cache can\n affect overall performance, and degrade system\n response times. Do not run cache flushes during\n business hours, and do not trigger cache flushes\n automatically.\n
ConditionsThe conditions under which the assignment rule\n applies. In the example, the assignment rule applies\n when an incident is in the\n Network category
Assign to
UserThe user the event is assigned to.
GroupThe group the event is assigned to.
Script
ScriptA script to specify advanced assignment rule\n functionality. The current.variable_pool set of\n variables is available. Make sure the input in the\n script is correct, and that the input type matches\n the field type in the Assignment Rule script. For\n example, if the assignment rule script sets the\n value of an Integer field, and the value in the\n script is set to String, the assignment rule may\n yield unexpected results.\n
Other\n fields
Match conditionsChoices are: \n
Execution OrderThe order in which the assignment rule is processed.\n If assignment rules conflict, a rule with a lower-order\n value takes precedence over a rule with a higher value.\n If the order values are set to the same number, the\n assignment rule with the first matching condition takes\n precedence over the others. Only the first assignment\n rule with a matching condition runs against a\n record.
  • Assignment lookup rules example
  • Assignment rules module
  • Condition editor example
  • Data lookup rules
  • Precedence between data lookup, assignment, and business rules
  • Workflow assignments
  • Define assignment rules
  • Configuring the form layout
  • Baseline assignment rules example
  • DSA with JS - Self Paced
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Operator
  • JS Projects
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter

How to fix SyntaxError – ‘invalid assignment left-hand side in JavaScript’?

In JavaScript, a SyntaxError : Invalid Assignment Left-Hand Side occurs when the interpreter encounters an invalid expression or structure on the left side of an assignment operator ( = ).

This error typically arises when trying to assign a value to something that cannot be assigned, such as literals, expressions, or the result of function calls. Let’s explore this error in more detail and see how to resolve it with some examples.

Understanding an error

An invalid assignment left-hand side error occurs when the syntax of the assignment statement violates JavaScript’s rules for valid left-hand side expressions. The left-hand side should be a target that can receive an assignment like a variable, object property or any array element, let’s see some cases where this error can occur and also how to resolve this error.

Case 1: Error Cause: Assigning to Literals

When you attempt to assign a value to a literal like a number, string or boolean it will result in SyntaxError: Invalid Assignment Left-Hand Side .

Resolution of error

In this case values should be assigned to variables or expressions which will be on the left side of an equation and avoid assigning values directly to literals.

Case 2: Error Cause: Assigning to Function Calls

Assigning a value directly to the result of function call will give an invalid assignment left-hand side error.

Explanation : In this example, getX() returns a value but is not a valid target for assignment. Assignments should be made to variables, object properties, or array elements, not to the result of a function call.

Therefore, store it into a variable or at least place it on the left-hand side that is valid for assignment.

author

Please Login to comment...

Similar reads.

  • Web Technologies

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IMAGES

  1. Invalid Left Hand Side in Assignment: Discover the Fix

    invalid assignment left hand side servicenow

  2. R Error : invalid (do_set) left-hand side to assignment (2 Examples)

    invalid assignment left hand side servicenow

  3. ServiceNow

    invalid assignment left hand side servicenow

  4. Task Assignment Rules in ServiceNow CSA Tutorial for Beginners

    invalid assignment left hand side servicenow

  5. Invalid left-hand side in assignment页面报错问题解决方法_html_程序编码_思韵闪耀_一生受益

    invalid assignment left hand side servicenow

  6. 29. Use cases of Assignment Rule in ServiceNow

    invalid assignment left hand side servicenow

COMMENTS

  1. SyntaxError: invalid assignment left-hand side - JavaScript | MDN

    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 ===.

  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])

  3. How to fix SyntaxError: invalid assignment left-hand side

    The JavaScript error SyntaxError: invalid assignment left-hand side occurs when you have an invalid syntax on the left-hand side of the assignment operator. This error usually occurs because you used the assignment operator = when you should be using comparison operators == or === .

  4. ServiceNow – The world works with ServiceNow™

    We would like to show you a description here but the site won’t allow us.

  5. Assignment Rule is not working - incidents are ... - ServiceNow

    Incidents are being created without any value populated in the Assignment Group field (Assignment Rule is failing).

  6. Unable to assign Problem tickets/record to users - ServiceNow

    Unable to assign the Problem records to the user i.e Reference qualifier is not displaying the user for selection.

  7. 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.

  8. Create an assignment rule - Product Documentation: Utah - Now ...

    Loading... Loading...

  9. JavaScript ReferenceError – Invalid assignment left-hand side">JavaScript ReferenceError – Invalid assignment left-hand side

    How to fix SyntaxError - 'invalid assignment left-hand side in JavaScript'? In JavaScript, a SyntaxError : Invalid Assignment Left-Hand Side occurs when the interpreter encounters an invalid expression or structure on the left side of an assignment operator (=).

  10. invalid assignment left-hand side ...">How to fix SyntaxError - 'invalid assignment left-hand side ...

    An invalid assignment left-hand side error occurs when the syntax of the assignment statement violates JavaScript’s rules for valid left-hand side expressions.