I have a very simple arithmetic operator but am at my wits end why it doesn’t return 2. The code below returns 1. I thought that x++ equates to x = x + 1;
CODE
var x = 1;
document.write(x++);
However if I run the code as follows, it returns 2 as expected
CODE
var x = 1;
document.write(++x);
What am I doing wrong?
PostIncrement(variable++) & PostDecrement(variable–)
When you use the
++or--operator after the variable, the variable’s value is not incremented/decremented until after the expression is evaluated and the original value is returned. For examplex++translates to something similar to the following:PreIncrement(++variable) & PreDecrement(–variable)
When you use the
++or--operator prior to the variable, the variable’s value is incremented/decremented before the expression is evaluated and the new value is returned. For example++xtranslates to something similar to the following:The postincrement and preincrement operators are available in C, C++, C#, Java, javascript, php, and I am sure there are others languages. According to why-doesnt-ruby-support-i-or-i-increment-decrement-operators, Ruby does not have these operators.