Skip to content Skip to sidebar Skip to footer

Why Expression As Left-hand Side In Assignment Doesn't Always Work?

This code works: And that makes me believe that any expression works as a left-hand side in assignment, however, this code doesn't work: What makes a left-hand side valid in an a

Solution 1:

The relevant bit of the specification is here:

It is a Syntax Error if AssignmentTargetType of LeftHandSideExpression is not simple.

and here.

The simplified version is that (if we leave destructuring aside) you can assign a value to a variable or to a property of an object.

Given (foo), the parenthesis are pointless and the expression is just a variable name. This is fine.

Given (foo1 && foo2 && foo3) the values are read from the variables, compared using the && operator and the outcome is a value.

You can't assign a value to a value, only to a property or a variable.


Post a Comment for "Why Expression As Left-hand Side In Assignment Doesn't Always Work?"