Regarding
p = 0;
(p+1)++;
> ReferenceError: Invalid left-hand side expression in postfix operation
and
p = 0;
++(p+4);
> ReferenceError: Invalid left-hand side expression in prefix operation
I just got a bit of a surprise, as I expected postfix/prefix operators to be ok with working on the resolution of the expression (brackets have the highest operator precedence).
Could someone give me with a line or three to explain what is happening here?
Thanks
EDIT: Thanks for the quick responses, first answer marked as the answer. I feel I should also point people to the indepth answer from @thefourtheye below
Regarding
p = 0;
(p+1)++;
> ReferenceError: Invalid left-hand side expression in postfix operation
and
p = 0;
++(p+4);
> ReferenceError: Invalid left-hand side expression in prefix operation
I just got a bit of a surprise, as I expected postfix/prefix operators to be ok with working on the resolution of the expression (brackets have the highest operator precedence).
Could someone give me with a line or three to explain what is happening here?
Thanks
EDIT: Thanks for the quick responses, first answer marked as the answer. I feel I should also point people to the indepth answer from @thefourtheye below
Share Improve this question edited May 23, 2017 at 12:20 CommunityBot 11 silver badge asked Apr 2, 2014 at 6:07 Ashley CoolmanAshley Coolman 11.6k5 gold badges62 silver badges87 bronze badges 2- 1 what is p here ?? a variable? – Dimag Kharab Commented Apr 2, 2014 at 6:09
-
1
first try to convert
p
in integer – joker Commented Apr 2, 2014 at 6:09
4 Answers
Reset to default 10++
increments the value of a variable, so it is larger than before. Eg:
var x = 3;
x++;
alert(x); // will show 4
For there to be any point for this, the expression to the left of ++
must be accessible and mutable, otherwise the increment would be possible. Eg:
3++
doesn't make any sense, as 3 is a constant and can't be incremented. We don't want this to be possible:
3++;
alert(3); // outputs 4???
This is why your expression doesn't work. Ie:
var p = 2;
(p + 1)++;
has the same problem as above. (p + 1)
will evaluate to 3, and ++
can't change the value of the constant 3.
You are trying to increment ()
, the increment/decrement
operator can be apply on variable, try the sample code
(p++) + 1
OR
(++p) + 1
Remember that when you write p++
, that actually gets translated to p = p + 1
. The operators ++
and --
are convenience notation for incrementing/decrementing a variable for future use. But how is (p+1)++
or ++(p+4)
supposed to be translated? Those sort of imply that 1
or 4
are being incremented/decremented for future use, which doesn't make sense.
When you have an expression like this
(expr)++;
These are the operations JavaScript will do internally
Resolve the actual object referenced by
expr
.This step is important, because you can even do something like this
var a = {b: 1}; ++a.b; a.b++; console.log(a.b); # 3
Now, JavaScript has to resolve the actual object to be incremented. In this case, it will be
b
ina
.Get the value at the reference and convert that value to a Number.
This step is also very important, because you may even have values like this
var a = {b: '1'}; console.log(++a.b); # 2
JavaScript will try its best to get a number value, instead of failing immediately.
Increment the number.
Store the new vale in
expr
. This is step where your expression is failing.In your case
expr
isp + 1
, when it is resolved the value would be just a numeral, whose value can never be changed. (You can never change the value of 1 to something else). So, after the incrementing part, when the new value has to be stored back, JavaScript doesn't find a valid reference to store it. That is why it throws this error.ReferenceError: Invalid left-hand side expression in postfix operation
This error message is actually thrown from internal PutValue
method. The very first step goes like this
If Type(V) is not Reference, throw a ReferenceError exception.
Reference: ECMA Script 5.1 Standard Specification for Prefix Increment Operator