- 2

Why I am getting 5?

I have a problem about how do I getting 5 as output on below code. please explain it. function test(number){ while(number<5){ number++; } return number; } alert(test(2));

5th Aug 2017, 11:08 AM
Nipun Shihara
Nipun Shihara - avatar
1 Antwort
+ 5
Because you do incrementation by one while 'number' is strictly lesser than five: last time 'number' is incremented is when number is equal to four, the condition is true, so incrementation is done ('number' now equal five), then then is false, loop end, and actual value of 'number' (5) is returned, so outputed in alert box ^^
5th Aug 2017, 11:13 AM
visph
visph - avatar