Why is JavaScript postfix notation different from C and Perl?

I am currently studying the JavaScript exam. I also gained a little knowledge of C and Perl, so I am familiar with the prefix and postfix notation in all three languages.

I did an online exam for this, and one mistake I made was to evaluate the following code:

var x = 10; x += x--; 

Now I thought that he would evaluate to 19, because it would be 10 + 10, and then subtract 1 to make 9. But the feedback I received was that it was wrong, and in fact it rated to 20. I thought this sounds a little suspicious, so I tested it in an HTML document and it came out with 20 again. Then I tried equivalents in C and Perl, and both were rated as 19.

Can someone explain to me why JavaScript rates the answer as 20 when other languages ​​rate it as 19? The answer I received from the test was not too clear:

++ increment and decrement - operators can be placed either before or after the operand. If an increment or decrement operator is placed before the operand, the operation is performed immediately. If the increment or decrement operand is placed after the operand, changing the value of the operand is not obvious until the next operand is available in the program. Thus, the expression x + = x-- is equivalent to x = x + 10, which evaluates to 20.

+6
javascript postfix-notation
source share
4 answers

Expression expansion

 x += x--; 

to more detailed JS code

 x = x + (function(){ var tmp = x; x = x - 1; return tmp; })(); 

the result makes sense, as it will evaluate

 x = 10 + (function(){ var tmp = 10; x = 10 - 1; return tmp; })(); 

which is 20. Keep in mind that JS evaluates expressions from left to right, including compound assignments, that is, the value of x cached before x-- executed.


You could also think of it this way: assuming the evaluation order is from left to right, JS analyzes the assignment as

 x := x + x-- 

while perl will use

 x := x-- + x 

I do not see any convincing arguments for or against any choice, so it’s just unlucky that different languages ​​behave differently.

+6
source share

In C / C ++, each variable can only be changed once in each statement (I think the exact terminology is: only once between two code points, but I'm not sure).

If you write

 x += x--; 

you change the value of x twice:

  • you decrease x using the postfix operator
  • you set x using assignment

Although you can write this and the compiler will not complain (I'm not sure you can check different warning levels), the result is undefined and may be different in each compiler.

+3
source share

In C line

 x += x--; 

- undefined behavior. It seems your specific compiler treats it like this:

 oldx = x--; x = x + oldx 

However, the ECMAScript specification indicates op= - and before evaluating the right margin, it gets the value on the left.

Thus, this will be equivalent to:

 oldx = x--; x = oldx + oldx 
+1
source share

In principle, the value of x decreases after assignment. This example may make it clearer (run in Firebug console)

 var x = y =10; x += y--; console.log(x , y); // outputs 20 9 
+1
source share

All Articles