Search code examples
javascripthtmlarraysobjectwhile-loop

how to print numbers using while loop in javascript


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

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

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++;
    }