In the JavaScript language
var a=3;
a += a -= a*a; //-3
In the C language:
int a=3;
a += a -= a*a; //-12
In the JavaScript language
var a=3;
a += a -= a*a; //-3
In the C language:
int a=3;
a += a -= a*a; //-12
In JS, operands of an operator such as +
, =
, *
or +=
are always evaluated left-to-right. So what happens here is, step by step:
var a = 3; // a = undefined
a += (a -= a*a); // a = 3
a = 3 + (a -= a*a); // a = 3
a = 3 + (a = 3 - a*a); // a = 3
a = 3 + (a = 3 - 3*a); // a = 3
a = 3 + (a = 3 - 3*3); // a = 3
a = 3 + (a = 3 - 9); // a = 3
a = 3 + (a = -6); // a = 3
a = 3 + -6; // a = -6
a = -3; // a = -6
-3; // a = -3
In contrast, your C code does evaluate the left-hand side a
only after the right-hand side operand is evaluated, at which point a
has a value of -6
, which is then added to itself. However, the C language leaves this as undefined behaviour and does not define an specific order for the evaluations, so you only experienced this as an artifact of your compiler. See @Kay's comments for details.
JavaScript assignments traverse left to right.
You are telling JS:
3 += 3 = 6
6 -= 3*3 = -3
I can't think of a permutation of this that would result in -12...