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;
C
I think of x++
and ++x
(informally) as this:
x++
:
function post_increment(x) {
return x; // Pretend this return statement doesn't exit the function
x = x + 1;
}
++x
:
function pre_increment(x) {
x = x + 1;
return x;
}
The two operations do the same thing, but they return different values:
var x = 1;
var y = 1;
x++; // This returned 1
++y; // This returned 2
console.log(x == y); // true because both were incremented in the end