++someVariable vs. someVariable++ in JavaScript

JavascriptIncrementUnary Operator

Javascript Problem Overview


In JavaScript you can use ++ operator before (pre-increment) or after the variable name (post-increment). What, if any, are the differences between these ways of incrementing a variable?

Javascript Solutions


Solution 1 - Javascript

Same as in other languages:

  • ++x (pre-increment) means "increment the variable; the value of the expression is the final value"
  • x++ (post-increment) means "remember the original value, then increment the variable; the value of the expression is the original value"

Now when used as a standalone statement, they mean the same thing:

x++;
++x;

The difference comes when you use the value of the expression elsewhere. For example:

x = 0;
y = array[x++]; // This will get array[0]

x = 0;
y = array[++x]; // This will get array[1]

Solution 2 - Javascript

  • ++x increments the value, then evaluates and stores it.
  • x++ evaluates the value, then increments and stores it.

var n = 0, m = 0;

alert(n++); /* Shows 0, then stores n = 1 */
alert(++m); /* Shows 1, then stores m = 1 */

Note that there are slight performance benefits to using ++x where possible, because you read the variable, modify it, then evaluate and store it. Versus the x++ operator where you read the value, evaluate it, modify it, then store it.

Solution 3 - Javascript

As I understand them if you use them standalone they do the same thing. If you try to output the result of them as an expression then they may differ. Try alert(i++) as compared to alert(++i) to see the difference. i++ evaluates to i before the addition and ++i does the addition before evaluating.

See http://jsfiddle.net/xaDC4/ for an example.

Solution 4 - Javascript

I've an explanation of understanding post-increment and pre-increment. So I'm putting it here.

Lets assign 0 to x

let x = 0;

Lets start with post-increment

console.log(x++); // Outputs 0

Why?

Lets break the x++ expression down

x = x;
x = x + 1;

First statement returns the value of x which is 0

And later when you use x variable anywhere, then the second statement is executed

Second statement returns the value of this x + 1 expression which is (0 + 1) = 1

Keep in mind the value of x at this state which is 1

Now lets start with pre-increment

console.log(++x); // Outputs 2

Why?

Lets break the ++x expression down

x = x + 1;
x = x;

First statement returns the value of this x + 1 expression which is (1 + 1) = 2

Second statement returns the value of x which is 2 so x = 2 thus it returns 2

Hope this would help you understand what post-increment and pre-increment are!

Solution 5 - Javascript

var a = 1;
var b = ++a;
alert('a:' + a + ';b:' + b); //a:2;b:2

var c = 1;
var d = c++;
alert('c:' + c + ';d:' + d); //c:2;d:1

jsfiddle

Solution 6 - Javascript

var x = 0, y = 0;

//post-increment: i++ returns value then adds one to it
console.log('x++ will log: ', x++); //0
console.log('x after x++ : ', x);    //1

//pre-increment: adds one to the value, then returns it
console.log('++y will log: ', ++y); //1
console.log('y after ++y : ', y);   //1

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDerek AdairView Question on Stackoverflow
Solution 1 - JavascriptJon SkeetView Answer on Stackoverflow
Solution 2 - JavascriptJustin ForceView Answer on Stackoverflow
Solution 3 - JavascriptChrisView Answer on Stackoverflow
Solution 4 - JavascriptunclexoView Answer on Stackoverflow
Solution 5 - JavascriptThe Code GuyView Answer on Stackoverflow
Solution 6 - JavascriptClifford FajardoView Answer on Stackoverflow