← Back to JavaScript Course | Chapter 4: Modern JS, Async & DOM | Lesson 6 of 26

JS Loop While

Think of pouring water into a glass, you keep pouring while there's still room left, checking the fill level again before every single pour, and you stop the moment that condition is no longer true. A while loop in JavaScript works the same way, it keeps running its block of code as long as a condition stays true, checking that condition before every single pass through the loop. while loops are especially useful when you don't know in advance exactly how many times you'll need to repeat something, like waiting for a specific response or condition while running scripts on cookiescursor.com.
Syntax
javascript
while (condition) {
  // loop body
}

do {
  // loop body
} while (condition);

The while Loop

A while loop checks its condition before running its block, and continues repeating that block for as long as the condition remains true, stopping the moment it becomes false.

Note: Always make sure something inside the loop body eventually changes the condition, otherwise the loop will never stop on its own.
Warning: If a while loop's condition never becomes false, the browser tab can freeze or crash from an infinite loop.

उदाहरण: The while Loop

javascript
// Declare the variable `i`, set to `0`
// Declare the variable `i`, set to `0`
let i = 0;
// Keep looping while `i < 3` holds
// Keep looping while `i < 3` holds
while (i < 3) {
  // Print `i` to the console
  // Print `i` to the console
  console.log(i);
  i++;
}

The do...while Loop

A do...while loop runs its block of code first, before checking the condition, guaranteeing the block executes at least once even if the condition is false from the very start.

Note: Use do...while specifically when you need guaranteed first execution, like showing a menu at least once before checking whether to repeat it.
Warning: Don't forget the semicolon after the closing while(condition) in a do...while statement, it's easy to omit by habit from regular while loops.

उदाहरण: The do...while Loop

javascript
let i = 5;
do {
  console.log(i);
  i++;
} while (i < 3); // runs once even though condition is false

while vs for

for loops are generally preferred when the number of iterations is known ahead of time, while while loops are better suited to situations where the stopping condition depends on something that can only be checked during the loop itself.

Note: If you find yourself writing let i = 0 before a while loop just to count iterations, a for loop is probably the more natural fit.
Warning: Choosing the wrong loop type doesn't cause errors, but it can make code less readable and harder for others to quickly understand its intent.

उदाहरण: while vs for

javascript
// for: iterations known ahead of time
for (let i = 0; i < 3; i++) console.log(i);
// while: stopping point depends on runtime state
let n = 10;
while (n > 1) n = n / 2;
console.log(n);

Avoiding Infinite Loops

An infinite loop occurs when a loop's condition can never become false, causing the code to run forever and typically freezing the browser tab, careful review of the loop's update logic is the best defense.

Note: Before running a new while loop, mentally trace through a few iterations to confirm the condition genuinely becomes false eventually.
Warning: An infinite loop in browser JavaScript can completely freeze the page, requiring a forced tab close, always double check loop conditions carefully.

उदाहरण: Avoiding Infinite Loops

javascript
// Avoid this - infinite loop, condition never becomes false:
// let i = 0;
// while (i < 5) { console.log(i); }
let i = 0;
while (i < 5) {
  console.log(i);
  i++; // update makes condition eventually false
}

When to Use while

while loops shine in situations like waiting for a condition to change during processing, repeatedly asking for valid input, or working through a data structure until it's empty, cases where the stopping point genuinely can't be known in advance.

Note: If your loop repeatedly checks 'has this specific thing happened yet', that's usually a strong signal a while loop is the right tool.
Warning: Overusing while for tasks that actually have a known, fixed iteration count adds unnecessary complexity compared to a straightforward for loop.

उदाहरण: When to Use while

javascript
// Declare the variable `queue`, set to `[1, 2, 3]`
// Declare the variable `queue`, set to `[1, 2, 3]`
let queue = [1, 2, 3];
// Keep looping while `queue.length > 0` holds
// Keep looping while `queue.length > 0` holds
while (queue.length > 0) {
  // Print `queue.shift()` to the console
  // Print `queue.shift()` to the console
  console.log(queue.shift());
}
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.