+ 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?

11th Dec 2016, 3:29 PM
Jimmy
Jimmy - avatar
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.
11th Dec 2016, 3:48 PM
Venkatesh(Venki)
Venkatesh(Venki) - avatar
+ 1
Thank you! Now I understand
11th Dec 2016, 3:52 PM
Jimmy
Jimmy - avatar
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
11th Dec 2016, 3:59 PM
Morpheus
Morpheus - avatar