Q. What happens in the following javascript code snippet?

Code:
var num = 1;
while (num < 15) 
{
     console.log(num);
     num++;
}
  • (A) An exception is thrown
  • (B) An error is displayed
  • (C) The values of count are logged or stored in a particular location or storage
  • (D) The value of count from 1 to 14 is displayed in the console
πŸ’¬ Discuss
βœ… 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.

Explanation by: Mr. Dubey

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.

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
270
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Kirti
Publisher
πŸ“ˆ
85%
Success Rate