Ad

How To Print Numbers Using While Loop In Javascript

- 1 answer

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?

Ad

Answer

Try this:

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

Ad
source: stackoverflow.com
Ad