In Javascript you can use ++
operator before or after the variable name. What, if any, are the differences between these ways of incrementing a variable?
|
||||
|
Same as in other languages:
Now when used as a standalone statement, they mean the same thing:
The difference comes when you use the value of the expression elsewhere. For example:
|
|||
Note that there are slight performance benefits to using |
||||
|
As I understand them if you use them standalone they do the same thing. If you try to output the result of them as an expression then they may differ. Try alert(i++) as compared to alert(++i) to see the difference. i++ evaluates to i before the addition and ++i does the addition before evaluating. See http://jsfiddle.net/xaDC4/ for an example. |
|||
|
I was thinking about this yesterday reading this response to the question about bad assumptions in C/C++. In all cases, can we guarantee that Javascript behaves this way? Or do you think it's bad practice to use the increment statement within a more complex statement at all? |
|||||
|
|
|||
|