4

This could be a very naive dumb question, but what is the difference in the output of the following 2 condition:

for (var p=0; p<3; p++) {console.log(p)}
//outputs:
0
1
2


for (var p=0; p<3; ++p) {console.log(p)}
//outputs:
0
1
2

'p' result into same output regardless whether I increment the value first and then print it or vice vera. also I do understand the diff between (p++) and (++p), but in this case I'm unable to understand whether at this point in looping will it make it any difference if I were I do either of 2 or if it does make difference how would that impact my program.

Can someone please explain.

Thank

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
user1234
  • 3,000
  • 4
  • 50
  • 102
  • In JavaScript, there is absolutely no difference between prefix and postfix `++` when you don’t use the value of the expression, ever. – Ry- May 18 '19 at 22:05
  • Most languages have documentation for their operators: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Arithmetic – David May 18 '19 at 22:05
  • It would make more sense to write the loop `for (var p=0; p<3; p+=1)` because it encodes your intentions more directly. You use `p++` or `++p` when you also intend to read the value of `p`. For example, `p++ > limit` or `--x === 0` – Mulan May 18 '19 at 22:07
  • thanks all for the prompt answers, so then why do we tend to use i++ more often while looping than ++i if there is no difference? – user1234 May 18 '19 at 22:11
  • 2
    @user1234: Convention. – Ry- May 18 '19 at 22:24
  • Possible duplicate of [++someVariable Vs. someVariable++ in Javascript](https://stackoverflow.com/questions/3469885/somevariable-vs-somevariable-in-javascript) – WilliamNHarvey May 20 '19 at 22:11

7 Answers7

3

If you dont use the values, after using pre- and post-fix, there is absolutely no difference at all. (exept from performance)

Doing something like this would behave differently, as you can see:

var a = 0;
var b = 0;
var arr = [0,1,2];

console.log(arr[++b]);
console.log(arr[b++]);
console.log(arr[b]);
DigitalJedi
  • 1,577
  • 1
  • 10
  • 29
  • “except from performance” implies there’s a performance difference, which there isn’t. – Ry- May 18 '19 at 22:24
2

In this case there is no difference whatsoever as you are not using the value of the expression.

0x76
  • 185
  • 3
  • 9
2

Since you're not assigning the expression to anything, there's no difference apart from a slight performance gain in the pre-incrementer (because a temporary variable is created in order to store the multiple values of p with the post-incrementer). JSBEN.CH.

Just so you know the difference between them:

let var1 = 3;
let var2 = 4;

console.log(var1++); //Outputs the value of var1, then increments it
console.log(++var2); //Increments the value of var2, then outputs it

The += syntax is actually better in this case, because it is easier to read, and it is just a compaction of p = p + 1 - literally no difference, performance-wise or otherwise. This means it's actually faster.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • @DigitalJedi See the snippet for more helpful information. – Jack Bashford May 18 '19 at 22:14
  • Im quite familiar with that, just didnt know there was a difference in performance of i++ and ++i; thx for that – DigitalJedi May 18 '19 at 22:17
  • I thought ++i was faster than i++ when evaluating any expression, as we wouldn't have the store the value of variable somewhere for increment, the ++i would do it right away w/o having to store the variable anywhere. – user1234 May 18 '19 at 22:19
  • “because a temporary variable is created in order to store the multiple values of p with the post-incrementer” isn’t true at the JavaScript level (no variable is involved) and isn’t true at the CPU level (it’s optimized away by the engine). – Ry- May 18 '19 at 22:26
1

If you put the two plus in front or after the variable/number only makes a different for evaluating it.

Because it is only inportant that the counter is incremented after leaving the block. If this is done before or after doesnt matter. At the end of the call the number is incremented equally.

Timo Reymann
  • 755
  • 5
  • 16
1

++p first it will count +1 and then return the result
p++ it will return the value and then add +1

p value will be different at the end of each turn in those 2 cases.

Gerpstarg
  • 125
  • 7
1

In your example there is no difference. However if you use the increment ++ or decrement -- operators inside a function the positioning is significant. To quote the article
JavaScript Increment ++ and Decrement --

If the operand is after the variable console.log displays a then it is incremented

let a = 1;
console.log(a++);    // 1
console.log(a);      // 2

If the operand is before the variable a it is incremented then console.log displays it

let a = 1;
console.log(++a);    // 2
console.log(a);      // 2

Several other languages such as C and C++ have the same behaviour.

However these operators need to be used with care. See the following stackoverflow answer (albeit it refers to JavaScript, but also applies to C etc)

Why avoid increment (“++”) and decrement (“--”) operators in JavaScript?

pjaj
  • 225
  • 4
  • 12
1

In this case there is no difference in pre and post increment. However in some cases like this can be significant:

here n and i are first evaluated and then incremented

var n=0
var i
for(i=0;n<5;n=i++){}

after the loop n and i look like this: n=5, i=6

here n and i are evaluated first, but i is incremented before entering the cycle

var n=0
var i
for(i=0;n<5;n=++i){}

after the loop n and i look like this: n=5, i=5

tronux7
  • 13
  • 6