0

Please can someone explain me this?

function test(number) { while(number < 5) { number++; } return number; } alert(test(2)); "The output is 5." I don't get it.

22nd Feb 2019, 5:32 AM
Khun Noon Zayar
Khun Noon Zayar - avatar
1 Answer
+ 1
It's a loop i.e while loop 1. while(2<5){ 2++; } after this it again goes to start of the loop until the number is equal to or greater than 5 2. while(3<5){ 3++; } 3. while(4<5){ 4++; } 4. Now we have ans = 5 which doesn't satisfies our condition. So it comes out of the loop and the function returns 5.
22nd Feb 2019, 5:50 AM
Akshay Madrikar
Akshay Madrikar - avatar