var a = 0;
(++a)+(a++)+(++a);
print(a);
This prints 3. I'm assuming it only executes single increment.
var a = 0;
(++a)+(a++)+(--a);
This prints 1. What's the rule to follow here?
Thank you.
var a = 0;
(++a)+(a++)+(++a);
print(a);
This prints 3. I'm assuming it only executes single increment.
var a = 0;
(++a)+(a++)+(--a);
This prints 1. What's the rule to follow here?
Thank you.
You're not assigning the outcome of your addition to anything. You do this:
(++a)+(a++)+(++a);
Which increments a
3 times. 0 + 3 = 3
so a
is the value 3.
JavaScript is executed left-to-right. You can see this by seeing what happens when you use multiplication
a = 1;
++a * a; // 4
// 2 * 2 = 4
a = 1;
a * ++a; // 2
// 1 * 2 = 2
a = 1;
a++ * a ; // 2
// 1 * 2 = 2
a = 1;
a * a++; // 1
// 1 * 1 = 1
After each of these, the resulting a
is 2
.