20

on my quest to learn and improve my JavaScript I came across a script that has a switch / case statement and I noticed some variables are incremented using ++ with the variable before the ++ and then some variables have the ++ after the variable. What's the difference between these? Here's an example of what I'm trying to explain notice the m and y variables.

 switch(f){
        case 0:{

            ++m;
            if(m==12){
                m=0;
                y++;
            }
            break;
        }
        case 1:{

            --m;
            if(m==-1){
                m=11;
                y--;
            }
            break;
        }
        case 2:{

            ++y;
            break;
        }
        case 3:{

            --y;
            break;
        }
        case 4:{

            break;
        }
        }
Mark Sandman
  • 3,293
  • 12
  • 40
  • 60
  • 2
    Does this answer your question? [++someVariable vs. someVariable++ in JavaScript](https://stackoverflow.com/questions/3469885/somevariable-vs-somevariable-in-javascript) – Ivar Nov 17 '20 at 15:30

6 Answers6

46

++i returns the value of i after it has been incremented. i++ returns the value of i before incrementing.

When the ++ comes before its operand it is called the "pre-increment" operator, and when it comes after it is called the "post-increment" operator.

This distinction is only important if you do something with the result.

var i = 0, j = 0;

alert(++i);  // alerts 1
alert(j++);  // alerts 0

One thing to note though is that even though i++ returns the value before incrementing, it still returns the value after it has been converted to a number.

So

var s = "1";
alert(typeof s++);  // alerts "number"
alert(s);  // alerts 2, not "11" as if by ("1" + 1)
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • 4
    And they are called pre-increment and post-increment operators. – ninjalj Jun 16 '11 at 21:13
  • 1
    Does pre-incrementing or post-incrementing in the for loop have any effect on the loop? These two cases: `for (i=0; i<5; i++)` vs `for (i=0; i<5; ++i)` – Mihailo Oct 12 '17 at 09:06
  • @Mihailo, no difference. I tend to pre-increment out of a habit learned writing C++; it used to be the case that pre-incrementing was a bit more efficient for some iterator types. AFAICT, there's no difference for JS. – Mike Samuel Apr 29 '18 at 18:37
13

The same difference as any other c-style ++ incrementor.

foo = ++i is the same as:

i = i + 1;
foo = i;

foo = i++ is the same as;

foo = i;
i = i + 1;
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
3
var i = 0;
alert('i is ' + (++i)); // i is 1

// i is now 1

var i = 0;
alert('i is ' + (i++)); // i is 0

// i is now 1
Connor Smith
  • 1,274
  • 7
  • 11
1

In JS (as well as C, Perl, and probably a dozen other languages), the ++i operator increments i before it evaluates the statement, and i++ increments it after. Same with --.

Example:

var i=1;
alert(i++);

Will show "1", but:

var i=1;
alert(++i);

Will show "2".

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
1

To illustrate, assuming:

var a = 1;

then

var b = ++a;

results in

true === (b === 2 && a === 2)

while

var b = a++;

results in

true === (b === 1 && a === 2)
Rob Raisch
  • 17,040
  • 4
  • 48
  • 58
0

++i is pre-increment, i++ is post-increment.

If you're only incrementing the variable and not using the result at the same time they are equivalent.

In which case you really should use i += 1 for readability (says Douglas Crockford..)

erlando
  • 6,736
  • 4
  • 24
  • 29