+ 5
Output of this code? Var x=5; y=x++; document.write(x++ - y--);
I don't get if the vars in the output line must be incremented or not. Thx for your help.
6 Answers
+ 7
x++: post-incrementation, ie evaluation first then incrementation
++x: pre-incrementation, ie incrementation first then evaluation
var x = 5;
document.write(x++); //writes 5, x is now 6
x = 5;
document.write(++x); //x is now 6, writes 6
Same deal for x-- and --x.
Regarding your code:
var x=5; //x is 5
y=x++; //y is 5, x is 6
document.write(x++ - y--); //writes 1 (6-5)
+ 5
y = x++ assigns 5 to y and then x gets incremented to 6...
when "document.write(x++ - y--);" it's executed, it first have to output the result before doing the increment-decrement... because is post-in(de)crement...
so, " document.write(x++ - y--);" will be " document.write(6-5);" and that throws aa output to the html of 1... after that, x gets incremented to 7 and y is decemented to 4...
Final result: the output is 1 and the final values of x and y in memory are 7 and 4
+ 1
Var x=5; //x=5 obvious
y=x++; //y=5 and x=6
document.write(x++ - y--); // increment x to 7 and decrement y to 4
// kk, got it, thx both.
+ 1
answer is 1.
0
y = x ++ ( x = 6 , y = 5)
x- y = 6 - 5= 1
- 1
answer is 3