+ 5

Can someone explain to me why the output is like this?

x=0; for(; x<=8; x++) { x += x - x++; console.log(x); } console.log(x+1); //output: 0 1 2 3 4 5 6 7 8 10

25th Apr 2019, 6:30 AM
Mo3n
Mo3n - avatar
2 Answers
+ 4
x=x+x-x++ thats what am gonna explain and the output outside the loop. x=0 so the equation is 0, in the equation x was increased by one, but it has no effect since the result is still coming back to x, so this goes on till it gets to 8, when x=8, 8 will be printed and then incremented which will be x=9 9<=8 so this will Break out of the loop with the value of x as 9, the last console.log(9+1) which is 10 I hope that helps
25th Apr 2019, 7:04 AM
✳AsterisK✳
✳AsterisK✳ - avatar
+ 4
*AsterisK* tnx a lot for clarification
25th Apr 2019, 8:22 AM
Mo3n
Mo3n - avatar