-1

Just wonder why the output by the while loop contains two "10".

var j=1;
while( j < 11){
    console.log(j);
    j ++;
}

The output looks like below 1 2 3 4 5 6 7 8 9 10 10

Just mention that do-while loop could produce similar problems, which helps to understand the nature of the last output.

var k=0
do{
    console.log("do-while");
    k++;
}while(k<10)

The output is like that: do-while do-while do-while do-while do-while do-while do-while do-while do-while do-while 9

Richard
  • 83
  • 2
  • 8

1 Answers1

2

You seem to be talking about JS Console's output or something similar?

First ten outputs (numbers form 1 to 10) are printed by the console.log command you have in your code.

And the last one is what "while" expression returns, and it's a last line of "while" block.

You can easier understand it if you read your code like:

var j=1;
while( j < 11){
    console.log(j);
    j++;
}
return j;
eckes
  • 10,103
  • 1
  • 59
  • 71
Jevgeni
  • 2,556
  • 1
  • 15
  • 18