How does the postfix y++ inside an equation (statement) work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How does the postfix y++ inside an equation (statement) work?

In JavaScript: What is the output of this code below and please explain it... How is this postfix working? var x=2; var y=1; y=x++; // y equals 2 or 3?? var z=x*y; document.write(z);

22nd Dec 2016, 10:36 PM
3TW3
3TW3 - avatar
2 Answers
+ 1
The output should be 6. The statement "y=x++" makes y=2 and x=3. This is because y was assigned the "original" value of x, which was 2 and then x was incremented (x++). Note that "y=x++" is totally different from "y=++x". If instead, "y=++x" was used in this code, y would then be assigned the value of x++, which is 3 and x would be incremented to 3 also. Which would give the output of 9 :]
22nd Dec 2016, 11:08 PM
Servant of The Most Merciful
Servant of The Most Merciful - avatar
+ 3
thank you very much!
23rd Dec 2016, 9:07 PM
3TW3
3TW3 - avatar