Skip to content

JS While Loop

Syntax

The while loop in JavaScript executes a block of code as long as the specified condition evaluates to true.

while (condition) {
// Code to execute
}

Example

let count = 1;
while (count <= 5) {
console.log(count);
count++;
}
// Output:
// 1
// 2
// 3
// 4
// 5