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 ===
.
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.
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.
if (Math.PI + 1 = 3 || Math.PI + 1 = 4) { console.log("no way!"); } // SyntaxError: invalid assignment left-hand side const str = "Hello, " += "is it me " += "you're looking for?"; // SyntaxError: invalid assignment left-hand side
In the if
statement, you want to use an equality operator (===
), and for the string concatenation, the plus (+
) operator is needed.
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 foo() { return { a: 1 }; } foo() = 1; // ReferenceError: invalid assignment left-hand side
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.
Optional chaining is not a valid target of assignment.
Instead, you have to first guard the nullish case.
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_assignment_left-hand_side