Q. What happens in the following javascript code snippet?
Code:var num = 1;
while (num < 15)
{
console.log(num);
num++;
}
β
Correct Answer: (D)
The value of count from 1 to 14 is displayed in the console
Explanation:
Let's analyze the JavaScript code snippet:
var num = 1;
while (num < 15)
{
console.log(num);
num++;
}
- The while loop starts with num = 1.
- The condition num < 15 is true initially.
- Inside the loop, console.log(num) prints the current value of num.
- Then, num++ increments num by 1 after each iteration.
- The loop continues until num reaches 15, at which point the condition num < 15 becomes false, and the loop stops.
- Therefore, the numbers from 1 to 14 will be printed in the console.
Thus, option (D) is correct.