javascripthtmlarraysobjectwhile-loop

how to print numbers using while loop in javascript


I have following JavaScript code segments to print less than 5 numbers between 1-10.

for (let i = 1; i < 10; i++) {
  if (i < 5) {
    document.write('The Number is ' + i + '<br>');
  }
}

Then I need print this using while loop. how can I do this?


Solution

  • Try this:

    let i = 1;
    while (i < 10) {
      if(i < 5) {
        document.write('The Number is ' + i + '<br>');
      }
      i++;
    }