Lesson 22 of 30 – JavaScript While Loop
60%

JavaScript While Loop

JavaScript includes the while loop to execute a block of code repeatedly as long as a specified condition remains true.

Note: Unlike a for loop, a while loop only requires a condition expression.
What is a While Loop?

A while loop repeatedly executes a block of code until the specified condition becomes false.

It is useful when the number of iterations is not known in advance.

Syntax of While Loop
while(condition){
  // code here
}

The loop continues until the condition evaluates to false.

Basic While Loop Example
Example:
var i = 0;

while(i < 5){
document.write(i + " ");
i++;
}
Output:

0 1 2 3 4

How While Loop Works
Step Description
1 Initialize variable before loop
2 Check condition
3 Execute code block
4 Update variable
5 Repeat until condition becomes false
Print Numbers 1 to 10
Example:
var i = 1;

while(i <= 10){
document.write(i + "<br>");
i++;
}
Output:

1 2 3 4 5 6 7 8 9 10

Using While Loop with Arrays

A while loop can be used to access array elements.

Example:
var arr=[10,20,30,40,50];
var i=0;

while(i < arr.length){
document.write(arr[i] + "<br>");
i++;
}
Output:

10
20
30
40
50

Infinite Loop Warning

Always update the counter variable inside the while loop.

Incorrect Example:
var i = 0;

while(i < 5){
document.write(i);
}

This creates an infinite loop because the value of i never changes.

What is Do While Loop?

JavaScript provides another variation called the do...while loop.

The do...while loop executes the code block first and checks the condition afterwards.

Therefore, the code block executes at least one time.

Syntax of Do While Loop
do{
  // code block
}
while(condition);
Do While Example
Example:
var i = 0;

do{
document.write(i + " ");
i++;
}
while(i < 5);
Output:

0 1 2 3 4

Do While Executes At Least Once

Even if the condition is false, the code block runs once.

Example:
var i = 5;

do{
document.write(i);
i++;
}
while(i < 1);
Output:

5

The loop executes once before checking the condition.

While vs Do While
While Loop Do While Loop
Condition checked first Condition checked last
May execute zero times Executes at least once
Entry controlled loop Exit controlled loop
Real-Life Example

A while loop can be used to count students present in a classroom.

Example:
var student = 1;

while(student <= 5){
document.write("Student " + student + "<br>");
student++;
}
Output:

Student 1
Student 2
Student 3
Student 4
Student 5

Advantages of While Loop
  • Useful when the number of iterations is unknown.
  • Simple and easy to understand.
  • Consumes less code for repetitive tasks.
  • Can be used with dynamic conditions.
  • Supports both increment and decrement operations.
  • Widely used in real-world programming.
Points to Remember
  • While loop executes code repeatedly while the condition is true.
  • The condition is checked before executing the loop body.
  • Initialization must be done before the loop.
  • Increment or decrement should be included inside the loop.
  • Improper conditions can create infinite loops.
  • do...while executes at least one time.
  • while is an entry-controlled loop.
  • do...while is an exit-controlled loop.
Key Points
  • while loop checks condition before execution.
  • do...while checks condition after execution.
  • Both loops are used for repetitive tasks.
  • Counter variables help control iterations.
  • Infinite loops should be avoided.
  • Loops make programs efficient and dynamic.
Quick Revision
Loop Type Condition Check
while Before execution
do...while After execution
Best Practices
  • Always initialize variables before loops.
  • Update counter variables correctly.
  • Avoid infinite loops.
  • Use meaningful variable names.
  • Keep loop conditions simple.
  • Use do...while when at least one execution is required.

🧠 Quick Quiz

Which loop executes at least one time even if the condition is false?