The code snippet is:
var a = 'foo'
a || a = 'bar'
What I expected is that it can work normally which means a
will be 'bar'
finally.But I got an error
Uncaught SyntaxError: Invalid left-hand side in assignment.
According to the error,I thought maybe it is something wrong with the left hand side expression a || a
.It seems that a || a
is invalid here.But why?I turn to ecmascript language specification for help.In 12.15.1 Static Semantics: Early Errors, I find:
It is an early Reference Error if LeftHandSideExpression is neither an ObjectLiteral nor an ArrayLiteral and AssignmentTargetType of LeftHandSideExpression is invalid.
So, the AssignmentTargetType of LeftHandSideExpression a||a
is invalid.But I am confused why a||a
's AssignmentTargetType is invalid.About this,the specification just said:
12.15.3 Static Semantics: AssignmentTargetType
AssignmentExpression:
YieldExpression
ArrowFunction
AsyncArrowFunction
LeftHandSideExpression = AssignmentExpression
LeftHandSideExpression AssignmentOperator AssignmentExpression
- Return invalid.
I can't figure out why a||a
's AssignmentTargetType is invalid based on what is given.
My question is: Why a||a = 'bar'
will get a reference error in javascript?If it is something with invalid AssignmentTargetType of LeftHandSideExpression,why a||a
is invalid?