2

After experimenting with the use of "i++" and "++i" I could not find a difference between their results when used in a 'for' loop. For example:

for (var i = 0; i < 10; ++i) {
    console.log(i);
}

would yield:

0
1
2
3
4
5
6
7
8
9

Shouldn't it be printing out the numbers from 1 to 10, as the iterator is being incremented before console.log(i) executes?

null
  • 2,060
  • 3
  • 23
  • 42
  • possible duplicate of [Pre increment in Javascript](http://stackoverflow.com/questions/23930661/pre-increment-in-javascript) – JAL Aug 14 '14 at 22:31

3 Answers3

4

The "increment step" is executed after the loop body is executed. Given

for (a;b;c) {
  d
}

the execution order is

a // initialize
b // condition - first iteration
d // loop body
c // "increment"
b // condition - second iteration
d // loop body
c // "increment"
...
b // condition - last iteration - break

So in your case:

var i = 0;
i < 10;
console.log(i); // 0
++i;
i < 10;
console.log(i); // 1
++i;
// ...
i < 10;

The difference between i++ and ++i is only relevant if you do something with the return value, which you don't.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Good answer. Also, an interesting note... Assuming i++ and ++i in javascript work similarly to C, ++i as a means to simply increment (such as in your example) is slightly quicker. ++i increments the value right away, while i++ actually has to make a copy of its value to support post-increments. – Jeff Aug 14 '14 at 22:52
  • 1
    @Jeff: Interesting. At least according to the ECMAScript specification there is not really a difference between those two: http://es5.github.io/#x11.3.1, http://es5.github.io/#x11.4.4. The steps are exactly the same safe for the last one. – Felix Kling Aug 14 '14 at 22:56
0

Because the last clause of the for loop only happens at the end of the loop, as its own statement, the behavior of your loop is not affected by this difference. However, imagine you did something like this:

for (var i = 0; i < 10;) {
    console.log(++i);
}
for (var j = 0; j < 10;) {
    console.log(j++);
}

Then you'd see a difference. The first example would produce numbers 1-10, whereas the second would produce numbers 0-9. That's because f(j++) is equivalent to j += 1; f(j);, whereas f(++i) is more like f(i); i += 1;.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
0

May I advise that while your testing is fine on whatever platform you are using, the standard construct is i++

Always code the standard and isolate various platforms and make exceptions as needed !!!

i++ Simply means increment 'i' by one.

I can speculate ++i means to add 'i' to itself eg if 'i' was 2 then it would then increment to 2,4,8,16,32

But I have never seen ++i used in many places.

Datadimension
  • 872
  • 1
  • 12
  • 31
  • Both `i++` (post-increment) and `++i` (pre-increment) are pretty standard operations in C like languages. Granted, the latter is not as common, but it's still standard. `i++` means "increment `i` by one and return the old value of `i`". `++i` means "increment `i` by one and return the new value of `i`". See http://stackoverflow.com/questions/3469885/somevariable-vs-somevariable-in-javascript – Felix Kling Aug 14 '14 at 22:54