views:

175

answers:

4

I'm studying for an exam on JavaScript at the moment. I've also got a little knowledge of C and Perl so I'm familiar with prefix and postfix notation in all three languages.

I did an online practice exam for it and one mistake I made was in evaluating the following code:

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

Now, I thought it would evaluate to 19 because it would be 10 + 10, then subtract 1 to make 9. But the feedback I got was that it was wrong and it actually evaluates to 20. I thought that sounded a bit suspicious so I tested it out in an HTML document, and it came out with 20 again. I then tried the equivalents in C and Perl and both evaluated to 19.

Can anyone explain to me why JavaScript evaluates the answer as 20 when other languages evaluate it to 19? The answer I got from the test wasn't too clear to me:

The increment ++ and decrement -- operators can be placed either before or after an operand. If the increment or decrement operator is placed before the operand, the operation occurs immediately. If the increment or decrement operator is placed after the operand, the change in the operand's value is not apparent until the next time the operand is accessed in the program. Thus the expression x += x-- is equivalent to x = x + 10 which evaluates to 20.

+3  A: 

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

If you write

x += x--;

you are changing the value of x twice:

  • you are decrementing x using the postfix -- operator
  • you are setting the value of x using the assignment

Although you can write this and the compiler won't complain about it (not sure, you may want to check the different warning levels), the outcome is undefined and can be different in every compiler.

Patrick
The term you're looking for is "sequence point", and it's significantly different from "every statement".
jamesdlin
Yes, you're right James. Thanks for pointing this out. More information on sequence points on Wikipedia: http://en.wikipedia.org/wiki/Sequence_point.
Patrick
+1  A: 

In C, the line

x += x--;

is undefined behaviour. It seems like your particular compiler is treating it like:

oldx = x--;
x = x + oldx

However, the ECMAScript specification does specify op= - and it gets the value of the left-hand-side before evaluating the right-hand-side.

So it would be equivalent to:

oldx = x--;
x = oldx + oldx
Anon.
+2  A: 

Expanding the statement

x += x--;

to the more verbose JS code

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

the result makes perfect sense, as it will evaluate to

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

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


You could also think of it this way: Assuming left-to-right evaluation order, JS parses the assignment as

x := x + x--

whereas Perl will use

x := x-- + x

I don't see any convincing arguments for or against either choice, so it's just bad luck that different languages behave differently.

Christoph
Thanks, that makes it reasonably clear - it's still annoying though. I guess it's there as a gotcha - I imagine no-one would actually use code like this unless it was deliberately obfuscated!
mattbd
+1  A: 

Basically, the value of x is decemented after assignment. This example might make it clearer (run in Firebug console)

var x = y =10;    
x += y--;        
console.log(x , y); // outputs 20 9
Russ Cam