1

I'm running this code:

var number = 0;
while (number <= 11) {
  console.log(number);
  number += 2;
}

But the output goes to 12. I've tried same with for loop the output was okay, till 10.

It tried it another way by replacing console.log with console.warn in while loop. The output warns till 10 but logs 12.

enter image description here

Further, by replacing console with alert, I got output till 10 in alert but still I see 12 in console.

enter image description here

j08691
  • 204,283
  • 31
  • 260
  • 272
nAiN
  • 55
  • 2
  • 7

1 Answers1

1

Console.log is not running one more time. In the console, it is just reporting the final value of number.

To prove this, simply prepend some text in console.log and you'll see.

var number = 0; 
while(number <= 11){   
    console.log("the output " + number);   
    number += 2; 
}

Dave Chen
  • 10,887
  • 8
  • 39
  • 67