Previous Next

JavaScript While Loop

The while loop in JavaScript is used to execute a block of code as long as a specified condition is true.

Syntax:

while (condition) {
  // block of code
}

Example:

let i = 1;

while (i <= 8) {
  console.log(i);
  i++;
}

Output:

1
2
3
4
5
6
7
8
Previous Next