+ 2
Can someone explain me why?
var a = 6; var b = 3; var z = a++ - --b; document.write(z); I will say that the result is 5, but is 4. Why? Does the --b don't count if to it's left is a sign? How can I make it work?
3 Answers
+ 3
First --b will execute because it's pre decrement, so now b is 2. a will not increment due to its post increment.
So z = 6 - 2 = 4. After this statement only a will increment.
Hope you understand this.
+ 1
Thank you! Now I understand
0
yep its 4,
a++ is post increment so a becomes 7 after finishing current statement , but in that line it's 6
but --b is pre decrement, it means decrement happens first then any operation so b becomes 2
so equation becomes
z = 6 - 2
but after this equation
a= 7