In javascript I have seen i++ used in many cases, and I understand that it adds one to the preceding value:
for (var i=1; i<=10; i++) {
console.log(i);
}
But what happens when I do this:
++i;
And is it any different using the -- operator (besides of course that it’s subtraction rather than addition)?
The difference between
i++and++iis the value of the expression.The value
i++is the value ofibefore the increment. The value of++iis the value ofiafter the increment.Example:
The
i--and--ioperators works the same way.