JavaScript while Loop

The while loop in JavaScript is designed for such scenarios where the number of repetitions isn’t fixed upfront and depends on a condition.

Unlike the for loop, which is typically used when you know how many times you need to iterate, the while loop keeps running as long as a condition is true. This makes it ideal for tasks where the stopping condition depends on dynamic factors, such as user input, data from a server, or other external factors.

What is the JavaScript while Loop?

The while loop runs a block of code repeatedly as long as a specified condition is true. It checks the condition before each iteration, and if the condition evaluates to false, the loop stops immediately.

Syntax

javascript
1
2
3
while (condition) {
  // code block to be executed
}
  • condition: A logical expression that determines if the loop should continue running.
  • Code block: The statements inside the loop that execute repeatedly while the condition is true.

Why Use the while Loop?

The while loop is useful when:

  1. You don’t know the exact number of times the loop should run.
  2. The loop needs to stop based on a condition that can change during execution.
  3. You want a simple and clear way to handle conditions dynamically.

Example: Basic while Loop

Here’s a simple example that adds numbers to a string while the value of i is less than 5:

javascript
1
2
3
4
5
6
7
8
9
let i = 0;
let text = "";

while (i < 5) {
  text += "The number is " + i + "\n";
  i++; // Increment to avoid infinite loop
}

console.log(text);

Output:

javascript
1
2
3
4
5
The number is 0
The number is 1
The number is 2
The number is 3
The number is 4

Important Notes

  • The loop will stop running once the condition becomes false.
  • If you forget to update the variable controlling the condition (e.g., i++), the loop will run forever, potentially crashing your program.

What is JavaScript do while Loop?

The do...while loop is a variation of the while loop. The key difference is that it executes the code block at least once, even if the condition is false, because the condition is checked after the code block runs.

Syntax

javascript
1
2
3
do {
  // code block to be executed
} while (condition);

Example: do while Loop

In this example, the code will run once before checking the condition:

javascript
1
2
3
4
5
6
7
8
9
let i = 5;
let text = "";

do {
  text += "The number is " + i + "\n";
  i++;
} while (i < 3);

console.log(text);

Output:

javascript
1
The number is 5

Even though i is already 5 and the condition i < 3 is false, the loop executes the code block once.

while Vs. for Loops

The while loop focuses on the condition, while the for loop provides initialization, condition, and increment in a single line. Both can achieve the same results, but their usage depends on the context.

Example: Using for Loop

javascript
1
2
3
4
5
6
7
8
const cars = ["BMW", "Volvo", "Ford"];
let text = "";

for (let i = 0; i < cars.length; i++) {
  text += cars[i] + "\n";
}

console.log(text);

Example: Using while Loop

javascript
1
2
3
4
5
6
7
8
9
10
const cars = ["BMW", "Volvo", "Ford"];
let i = 0;
let text = "";

while (i < cars.length) {
  text += cars[i] + "\n";
  i++;
}

console.log(text);

Both loops produce the same output:

javascript
1
2
3
BMW
Volvo
Ford

Key Takeaways

  • Use while when you don’t know how many times the loop should run, but you know the stopping condition.
  • Use do...while if you want the code block to run at least once, even if the condition is false.
  • Always update the variable controlling the condition to avoid infinite loops.
  • For fixed iterations, prefer the for loop for better readability.

Frequently Asked Questions