ScanSkill

while

The while statement creates a loop that executes as long as the condition is true.

Syntax

while(condition) {
  // Statements;
}

In the while statement takes in a condition, if the condition is true the statement wrapped inside parenthesis runs.

Example

let i = 1;

while (i < 5) {
  console.log(i);
  i++;
}
/* Prints:
1
2
3
4
5
*/

The while checks the condition before executing the statement. It is mostly used with break statements. This loop is used in cases where we don't know the end condition for ending the block of statement.