+ 6

# Here, the output is 9, however I don't see anything related to 9 or how it could give an output of 9. How is it possible??

var x = 6; var y = 0; while(x>0 && y<6) { x--; y += x; } document.write(y);

3 Answers

+ 4

If we run through the loop, it's easier to understand.
x = 6
y = 0
x-- //x = 5
y += x // y = 5
/*next iteration*/
x-- // x = 4
y += x // y = 9
y is bigger than 6, so the loop stops there

+ 4

Thanks so much Airree for that vivid explanation

+ 1

not understand