OneBite.Dev - Coding blog in a bite size

do a while loop in Javascript

Code snippet on how to do a while loop in Javascript

  let i = 0;
  while (i < 5) {
   console.log('The number is ' + i);
   i++;
  }

This code is using a while loop to print out the numbers 0 - 4. The code starts with declaring a variable ‘i’ and assigning it the value 0. Following this, the while loop is defined, stating that it should run while ‘i’ is less than 5. Inside the loop, the console.log method is used to output the result ‘The number is ’ plus the value of ‘i’. After this, ‘i’ is increased by one, as a result of the i++ statement, allowing the loop to continue as long as the condition remains true. After ‘i’ increases to 5, the loop is finished and the code continues from the next line.

javascript