6

I was having a conversation about the prefix increment operator, and we seem to have run into a disagreement.

When running this code:

var x = 0;
x = ++x;

is the second line equivalent to:

  • x = (x = x + 1) OR
  • x = (x + 1)

It is hard to tell the difference because the results are identical (both result in x having a value of 1)

I believe that the value is not saved to the original variable when the left hand side of the assignment is the variable itself.

My counterpart disagrees and thinks the value is saved to the original variable whenever the ++ operator is used.

Which one of us is right?

asked Nov 26, 2012 at 12:50

4 Answers 4

8

It is saved, so it is similar to the first example. Take for example this code:

var v = 0;
v = ++v + ++v + ++v;
// Returns 6

That is because this will translate to:

v = (0+1) + ((0+1)+1) + (((0+1)+1)+1);

Or, to be more accurate:

v = 0+1 +
v = 1+1 + //Previous value of v + 1
v = 2+1 //Previous value of v + 1

Why?

++v will first save the incremented value of v, then it will return this incremented value.
To simplify things, try this in your console:

x = 0;
++x;

If ++x would resolve to x + 1, the value of x would now still be 0, right?
Nope, your x will be 1. This means that ++x must have a assignment operator in there.

answered Nov 26, 2012 at 12:52
Sign up to request clarification or add additional context in comments.

4 Comments

I am not saying ++x resolves to x+1. I'm saying it resolves to x=x+1, but when it is on the right hand side of an operation the assignment does not take place.
@Asad: the assignment always takes place, regardless of where it is written: it's an expression all in itself. JS, like almost any other language, will first resolve all expressions within a statement, from the inside out. the difference is: ++x resolves to the incremented value, whereas x++ resolves to the value of x prior to it being incremented, just try x=0, then x++ and finally x in your console: 0, 0(<--old value) and 1 will be the result. then try ++x, it'll show 2 right away: the returned value is the new value when the increment operator comes first
But if the assignment didn't take place, v = ++v + ++v; would return 2, yet it returns 3.
@EliasVanOotegem That's what I was trying to say, yea, the comment was in reply to Asad
3

Just try writing both ++x and x++ out in full English sentences:
++x: increment x by one and return the value
x++: return the value of x, and increment it.

Your second line (x = ++x;) is equivalent to x = (x += 1), yes.
Just look at any loop you've ever written:

for (var i = 0;i<100;i++)//<-- no need for another assign here

So you could've written ++x; all the same, but since that expression is the entire statement, it makes no difference if you write x++; or ++x...

As you probably know xxsomeVar increments the variable by 1, assigns the resulting value to that variable and then returns it, someVar++ returns the current value of the variable, and then increments it by 1 (the new value is assigned to the variable, too, of course).

So ++x; is the equivalent of x = (x + 1);

From the Language specs:
++prefix increment operator
Postfix++ increment operator

answered Nov 26, 2012 at 13:11

Comments

3

Doing:

var x = 0;
console.log(++x); // will make print 1

Doing :

var x = 0;
console.log(x++); // will make print 0
console.log(x); // will print 1
answered Nov 26, 2012 at 12:52

3 Comments

Not entirely correct ` x = x++;` will assign 1 to x, but then reassign the old value (0) to x again: JS will first attempt to resolve all expressions to a value, and x++ is resolved to 0, and then assigns +=1 to x, but the resolved value of 0 is assigned to x at the end...
Yes. You're right. Sorry. I was referring to the current executing statement only.
Makes no difference: the statement is composed out of 2 expressions, both of which have to be executed, and both of them assign new values. It's the order in which they do that determines the overall value of x, in the first case x is assigned 1 twice, in the second example x is being assigned 1, and then 0
1

After reading the ECMAScript Language Specification in answer to the question it appears that ++x is equivalent to x = ( x = x + 1 ).

Translation of the steps outlined by the specification is:

  1. The result of the operation will be assigned to x
  2. Throw error if certain conditions are true
  3. oldValue = x
  4. newValue = oldValue + 1
  5. assign newValue to x
  6. return newValue

For the post-increment operator the above will return the oldValue instead of the newValue.

var x = 0;
// x is assigned 1 during the post-increment operation but because
// the oldValue is returned it is immediately replaced by 0.
x = x++; 
console.log( x ) // prints 0
answered Nov 26, 2012 at 13:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.