Skip to content

Latest commit

 

History

History
31 lines (26 loc) · 825 Bytes

while-loop.md

File metadata and controls

31 lines (26 loc) · 825 Bytes

JavaScript While Loop

The JavaScript while loop is used to repeatedly execute a block of code as long as a specified condition is true. It is one of the fundamental control flow structures in JavaScript.
The while loop provides a flexible way to repeat a block of code based on a condition.

Syntax

while (condition) {
  // code to be executed
}

Example

let count = 0;

while (count < 5) {
  console.log(`Current count: ${count}`);
  count++;
}

console.log("Loop finished!");

Infinite Loop

when using while loops to avoid creating infinite loops. An infinite loop occurs when the condition always evaluates to true, leading to continuous execution of the loop.

// Caution: Infinite Loop
while (true) {
  console.log("This is an infinite loop!");
}