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

17th Sep 2016, 9:20 AM
ricmic
ricmic - avatar
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)
17th Sep 2016, 1:40 PM
Zen
Zen - avatar
+ 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
17th Sep 2016, 12:31 PM
Nelson Urbina
Nelson Urbina - avatar
+ 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.
17th Sep 2016, 11:16 AM
ricmic
ricmic - avatar
+ 1
answer is 1.
17th Sep 2016, 9:41 PM
Š”ŠµŃ€Š³ŠµŠ¹ Š”Š°Ń€Š±Š°Ńˆ
Š”ŠµŃ€Š³ŠµŠ¹ Š”Š°Ń€Š±Š°Ńˆ - avatar
0
y = x ++ ( x = 6 , y = 5) x- y = 6 - 5= 1
17th Sep 2016, 12:03 PM
Sezgin GƜL
Sezgin GƜL - avatar
- 1
answer is 3
17th Sep 2016, 11:02 AM
Shokhrukh Soleev
Shokhrukh Soleev - avatar