0

I'm currently learning ES6 at JavaScript.info, and it says about the increment/decrement operators:

The prefix form returns the new value while the postfix form returns the old value (prior to increment/decrement)...

If we’d like to increase a value and immediately use the result of the operator, we need the prefix form...

If we’d like to increment a value but use its previous value, we need the postfix form.

But I'm confused as to when it would ever be necessary to use the previous value of a variable after it's been incremented? Would it be safe to assume that wherever it is, these are edgecases and the prefix form ++counter should almost always be preferred, or is there actually a good reason that the postfix counter++ form is more commonly seen out in the wild?

4
  • 1
    the postfix and prefix increment/decrement operators were a part of the language before ES6 Commented Oct 4, 2020 at 18:14
  • 1
    FWIW, those operators have existed since forever, he are not new to ES6. And the exists in other languages as well. Commented Oct 4, 2020 at 18:14
  • It's up to you, you should use the one you need for the case you have Commented Oct 4, 2020 at 18:15
  • Edited accordingly, thanks. Commented Oct 4, 2020 at 18:16

2 Answers 2

3

But I'm confused as to when it would ever be necessary to use the previous value of a variable after it's been incremented?

The idea is to (1) use the previous value, and then (2) increment the variable. For example, you might use it like this:

let index = 0;

const obj1 = { id: index++ };
const obj2 = { id: index++ };

console.log(obj1);
console.log(obj2);

Sometimes, that's what you need. Other times, you might want to extract the value after the increment is done, eg:

let highestIndexSoFar = 0;

const obj1 = { id: ++highestIndexSoFar };
const obj2 = { id: ++highestIndexSoFar };

console.log(obj1);
console.log(obj2);
console.log(highestIndexSoFar);

It depends on the situation. Both have their place.

Though, another option is to avoid them and instead increment only as a standalone statement, outside of an expression context - some may find that to be more readable instead:

let highestIndexSoFar = 0;

highestIndexSoFar++;
const obj1 = { id: highestIndexSoFar };
highestIndexSoFar++;
const obj2 = { id: highestIndexSoFar };

console.log(obj1);
console.log(obj2);
console.log(highestIndexSoFar);

Sign up to request clarification or add additional context in comments.

2 Comments

To clarify, when incrementing as a standalone statement, don't the usual rules about the prefix and postfix operators apply?
They do, but the rules don't matter, because the resulting expression isn't being used; all that it does is increment the variable. Whether one uses postfix or prefix has no effect in such a situation.
0

Would it be safe to assume that wherever it is, these are edgecases and the prefix form ++counter should almost always be preferred, or is there actually a good reason that the postfix counter++ form is more commonly seen out in the wild?

You would find more pre decrement usage out in the wild than post increment but adding to provided answer above, It is generally a better idea not to use pre/post increment/decrement if possible and use shorthand plus/minus operators like so:

counter += 1 ; counter -= 1

The idea being that it's just more developer friendly when it comes to refactoring and also it prevents you from odd looking code. I have seen this a lot that people use double x++ instead of x += 2 which is arguably more cleaner way of coding.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.