var x = 5;
var y = -1;
z = x++ + ++y / 2;
console.log(z);
Why the result is 5 and not 3.5? I start learning like one week ago so I'am a begginer
var x = 5;
var y = -1;
z = x++ + ++y / 2;
console.log(z);
Why the result is 5 and not 3.5? I start learning like one week ago so I'am a begginer
Division operator runs after addition operator, so your operation evaluates to:
z = (x++) + ((++y) / 2);
Hence, 5 + (0 / 2) = 5 + 0 = 5
.
I don't know where you would get 3.5, but as for the reason for the answer being 5 :
The post-increment operator (n++) returns the value and then increments. So, in your expression x++
is 5 but after your expression runs the value of x is 6.
The pre-increment operator (++n) increments the value then returns it. So, in your expression ++y
= 0.
From there it is just basic arithmetic.