6

For example this:

var a = 123;
var b = a++;

now a contains 124 and b contains 123

I understand that b is taking the value of a and then a is being incremented. However, I don't understand why this is so. The principal reason for why the creators of JavaScript would want this. What is the advantage to this other than confusing newbies?

random_user_name
  • 25,694
  • 7
  • 76
  • 115
Chad
  • 2,365
  • 6
  • 26
  • 37
  • 3
    There is no difference between Javascript and PHP in the behavior of the increment operator. – Asaph Dec 28 '09 at 07:38
  • 1
    Although you may be writing this in JavaScript, this behavior is the same as most (any?) C-style language. – Justin Johnson Dec 28 '09 at 07:41
  • Thank you all for your response. So this must have something to do with Object Oriented Programming. Having used PHP for so long, in the procedural fashion, I've come to expect that whenever a variable is on the left side of any operator, it will always keep it's previous value, unless it has been directly reassigned. This is a radically different way of thinking for me, and one that I will now have to get used to. – Chad Dec 28 '09 at 07:43
  • 2
    @Chad Actually no, it has nothing to do with OOP. PHP behaves the exact same way. It's about expressions resulting in values. – deceze Dec 28 '09 at 07:52
  • Scratch the PHP part, i've tested and it does infact behave the same way. – Chad Dec 28 '09 at 07:54
  • @Chad No disrespect intended; however, the post and pre-increment operators are pretty basic concepts. It appears that you are missing out on many fundamentals of programming such as differentiating operators from paradigms. I'd simply encourage you to take some time to get back to basics to come to a deeper understanding of the tools that you're using. – Justin Johnson Dec 28 '09 at 08:28

7 Answers7

9

That's why it's called the "post-incrementing operator". Essentially, everything is an expression which results in a value. a + 1 is an expression which results in the value 124. If you assign this to b with b = a + 1, b has the value of 124. If you do not assign the result to anything, a + 1 will still result in the value 124, it will just be thrown away immediately since you're not "catching" it anywhere.

BTW, even b = a + 1 is an expression which returns 124. The resulting value of an assignment expression is the assigned value. That's why c = b = a + 1 works as you'd expect.

Anyway, the special thing about an expression with ++ and -- is that in addition to returning a value, the ++ operator modifies the variable directly. So what happens when you do b = a++ is, the expression a++ returns the value 123 and increments a. The post incrementor first returns the value, then increments, while the pre incrementor ++a first increments, then returns the value. If you just wrote a++ by itself without assignment, you won't notice the difference. That's how a++ is usually used, as short-hand for a = a + 1.

This is pretty standard.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    Good answer. My thinking was: why if a = 2 and b = a + 1 we get b = 3 and a keeps its original value. But autoincrement is SPECIAL feature in programming languages that DOES NOT JUST MEAN ADD 1 and that is where I was confused, but now I get it. – Chad Dec 28 '09 at 07:53
5

Note that you can also write

b = ++a;

Which has the effect you are probably expecting.

It's important to realise that there are two things going on here: the assignment and the increment and the language should define in which order they will happen. As we have available both ++a and a++ it makes sense that they should have different meanings.

For those of us from a C background, this is quite natural. If PHP behaves differently, we might be wondering why PHP chose to deviate from what we are accustomed to.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
djna
  • 54,992
  • 14
  • 74
  • 117
4

++ can be used as post-increment operator like in your example, or it could be used as a pre-increment operator if used before variable.

var b = ++a;

Then first the variable a will be incremented, then the incremented value is assigned to b.

joar
  • 15,077
  • 1
  • 29
  • 54
Murat Can ALPAY
  • 520
  • 2
  • 17
1

This is the standard way of doing it. The postincrement operator assigns the value and then increments.

The preincrement (++a) operator increments and then assigns.

I am not familiar with php and cannot say how it does it or why.

mopoke
  • 10,555
  • 1
  • 31
  • 31
0

When you put the ++ after the variable, it gets incremented after the assignment. You can also put the ++ before the variable and it gets incremented before the assignment.

Javascript actually behaves exactly the same way as PHP for prefix and postfix incrementing.

Asaph
  • 159,146
  • 25
  • 197
  • 199
0

++ before variable call pre-increment means increment the value of variable before executing the statement.
++ after variable called post-increment means increment the value of variable after executing the statement.

both increments the value of variable.

$b=$a++;is equivalent to

$b=$a;//after
$a=$a+1;

$b=++$a;`is equivalent to

$a=$a+1;//before
$b=$a;

Another example

$a=5;
echo $a++;//prints 5;

$a=5;
echo ++$a;//prints 6;
Shaiful Islam
  • 7,034
  • 12
  • 38
  • 58
-3

Post-increment and pre-increment are common operators in many languages, Javascript being about 30 years from being the first. PHP supports post-increment too.

Ether
  • 53,118
  • 13
  • 86
  • 159