0

i have got different value when i++ is before the statement and when i++ is after the statement

var i = 0; while ( i<=5) { document.write(i + "<br />"); i++; } outputs = 0,1,2,3,4,5 var i = 0; while ( i<=5) { i++; document.write(i + "<br />"); } outputs 1, 2 , 3, 4, 5, 6. why are the results different?

1st May 2017, 10:02 PM
Haikal Jama Shaywal
3 Answers
+ 9
In the first case you increment i after you display it. So it starts at 0 since i is defined as 0. In the second case i is incremented before you display it, so when you call document.write() it is at 0+1 = 1. It then starts at 1.
1st May 2017, 10:19 PM
Karl T.
Karl T. - avatar
+ 7
Yes much like it.
2nd May 2017, 1:43 AM
Karl T.
Karl T. - avatar
0
okay thanks you very much. is it like. var x = 3; document.write(x++); outputs 3. var x = 3; x++; document.write(x); outputs 4.
2nd May 2017, 1:26 AM
Haikal Jama Shaywal