What's the difference between a++ and ++a?

What's the difference between a++ and ++a?
0

#1

I’ve looked up this answer elsewhere but it just isn’t really clicking for me, so I’m hoping maybe someone can help me understand.

I understand that both ++a and a++ means there’s an increment of one, but what’s the difference between the two?


#2

One increments before it’s evalutated, the other after.

var a = 1;

console.log("first a = " + (a++)); // evaluates, then increments
console.log("second a = " + a);

// first a = 1
// second a = 2

var b = 1;

console.log("first b = " + (++b)); // increments, then evaluates
console.log("second b = " + b);

// first b = 2
// second b = 2

Putting the ++ before causes it be be incrimented before it returns a vale. putting it after causes it to return a value, then increment it after it is done with it.


#3

The only difference is that ++a increment of one before you use it in an expression, and a++ do it latter.

Here are some examples:

var arr = ['banana', 'apple'];
var a = 0,
      b = 0;
arr[a++];        // print: banana  ==> arr[0]
arr[++b];       // print : apple  ==> arr[1]

#4

Okay, I think I get it.

But how do you know when to use which?


#5

I made a small pen to show you how it works

https://codepen.io/cjokinen/pen/aJXGwb?editors=1111

note that the variable is updated after console.log sends the output and the n variable is before.


#6

How do you know which to use?

It depends on what you need.

while (notDone) {
  //
  // other code ...
  //
  factor = a;
  a++; // or ++a, doesn't matter since it's in a statement by itself
}

In this case, since the last line (the increment) doesn’t get checked immediately so it doesn’t matter. That last line could have been factor = a++;. It would shorten the code a little, and would be easy enough to understand. But putting ++a would have a very different effect - a would be incremented first, and then assigned to factor.

If your code was this:

while (notDone) {
  //
  // other code ...
  //
  a++; // or ++a, doesn't matter since it's in a statement by itself
  factor = a;
}

Then, writing factor = ++a; would be the right thing to do because you need a to be incremented first.

To sum up, if the increment is alone in the statement, then it doesn’t matter which you use (but a++ would probably be more common.)

If it’s part of a complex statement, then you have to ask yourself - do I want it incremented before the formula is calculated or after? If you’re unsure, write it out as a separate statement and see where you put it.


#7

It’s a lazy shortcut, generally used in simple or complex expressions, like : arr[i++]; or arr.splice(++j, arr.length);
It’s void you to write an extra line :

arr[i];
i = i + 1;

for the first case.

j = j + 1;
arr.splice(j, arr.length);

for the latter.


#8

I don’t think it’s fair to call it “lazy”.

If I remember from my days of doing assembly (back when dinosaurs roamed the Earth), the increment operator is a little faster, having a specific instruction for it and could do it in situ. Writing it as “i=i+1” involves copying data into memory slots and then adding, then assigning that result to the original slot.

I don’t know how it’s implemented in JS (probably left up to the operating system) and computers are so fast nowadays that it doesn’t matter much, (especially if you’re just running a little loop on a web page), but it used to be an important distinction - imagine running some complicated lunar lander calculations on a vacuum tube machine.

But today it does remain an easy shorthand. Personally I prefer i++ to i=i+1 because my eye can recognize what it is a little faster. i would agree that sometimes you have to be a little careful - it’s easy to create visually confusing code if you stack too many operations in one line (I used to make nasty stuff like that all the time in C, before a coworker slapped me around and set me straight.)


#9

Yeah I agree, me too I prefer i++, much more easy to read and less code. The term lazy was an expression meaning shortcut in CS (I hear that everywhere so why not use it).

Concerning the performance, I wasn’t aware of that and it’s reinforce me the thought that laziness is the path of success in coding :sunglasses:. Thank you for sharing.