Table of contents
1.
Introduction
2.
Syntax
2.1.
Explanation:
3.
Using While Loop to Traverse an Array
3.1.
Output:
3.2.
Explanation:
4.
Do-While Loop
4.1.
Explanation:
5.
Example: Using Do-While Loop
5.1.
Output:
5.2.
Explanation:
6.
Comparison Between While and Do-While Loop
7.
Example: While vs Do-While
8.
Browser Compatibility
9.
Frequently Asked Questions
9.1.
When should I use a while loop instead of a for loop?
9.2.
What is the main difference between the while and do-while loops?
9.3.
Can I break out of a while or do-while loop?
10.
Conclusion
Last Updated: Dec 22, 2024
Easy

JavaScript While Loop

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaScript provides various loops to execute a block of code multiple times, and the while loop is one of them. It repeatedly runs a block of code as long as a specified condition is true. 

JavaScript While Loop

This article will cover everything you need to know about the while loop, including its syntax, usage with examples, the do-while loop, and a comparison of the two. 

Syntax

The syntax of a while loop is quite simple:

while (condition) {
    // Code to execute
}


Explanation:

  1. Condition: The loop runs only if this condition evaluates to true. If it’s false initially, the loop won’t execute even once.
     
  2. Code Block: The code inside the {} will execute repeatedly as long as the condition is true.

Using While Loop to Traverse an Array

The while loop is commonly used to traverse arrays when you want to iterate through all elements without using a for loop. Here’s an example:

let numbers = [10, 20, 30, 40, 50];
let i = 0;

while (i < numbers.length) {
    console.log(numbers[i]);
    i++;
}
You can also try this code with Online Javascript Compiler
Run Code


Output:

10
20
30
40
50


Explanation:

  1. Initialization: We declare a variable i and set it to 0.
     
  2. Condition: The loop checks if i is less than the length of the array.
     
  3. Execution: It prints the current element and increments i by 1.
     
  4. Termination: When i equals numbers.length, the condition becomes false, and the loop stops.

Do-While Loop

The do-while loop is a variation of the while loop. It ensures the code inside the loop runs at least once, even if the condition is initially false.

Syntax

The syntax for the do-while loop is as follows:

do {
    // Code to execute
} while (condition);


Explanation:

  1. The code block runs once before the condition is checked.
     
  2. If the condition is true, it repeats the code block. Otherwise, the loop exits.

Example: Using Do-While Loop

Here’s an example of the do-while loop:

let count = 1;

do {
    console.log("Number: " + count);
    count++;
} while (count <= 5);
You can also try this code with Online Javascript Compiler
Run Code


Output:

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5


Explanation:

  1. The code block prints the current value of count and increments it.
     
  2. The condition checks if count is less than or equal to 5.
     
  3. Even if the initial value of count was greater than 5, the loop would run once because the condition is checked after execution.

Comparison Between While and Do-While Loop

Both while and do-while loops have their advantages depending on the scenario. Here's a comparison:

ParametersWhile LoopDo-While Loop
Execution ConditionThe condition is checked before the code runs.The code runs once, even if the condition is false.
UsageUseful when you need to repeat only if a condition is met.Useful when you need to ensure at least one execution.
Example ScenarioLoop through an array if it’s not empty.Display a message before checking user input.

Example: While vs Do-While

Using a while loop:

let x = 5;

while (x > 10) {
    console.log("This will not run because x is less than 10.");
}
You can also try this code with Online Javascript Compiler
Run Code

 

Using a do-while loop:

let x = 5;

do {
    console.log("This will run at least once, even though x is less than 10.");
} while (x > 10);
You can also try this code with Online Javascript Compiler
Run Code


Output:

  • The while loop produces no output because the condition is false at the start.
     
  • The do-while loop prints the message once.

Browser Compatibility

Both while and do-while loops are supported across all modern and older browsers, making them reliable for client-side scripting. Here’s a quick compatibility chart:

BrowserWhile LoopDo-While Loop
Google ChromeSupportedSupported
Mozilla FirefoxSupportedSupported
SafariSupportedSupported
EdgeSupportedSupported
Internet ExplorerSupportedSupported

Frequently Asked Questions

When should I use a while loop instead of a for loop?

Use a while loop when the number of iterations isn’t known beforehand and depends on a condition being true.

What is the main difference between the while and do-while loops?

The while loop checks the condition before executing the code, whereas the do-while loop checks the condition after executing the code once.

Can I break out of a while or do-while loop?

Yes, you can use the break statement to exit a while or do-while loop prematurely.

Conclusion

The while and do-while loops are essential tools in JavaScript for executing code repeatedly based on a condition. The while loop checks the condition first, making it suitable for pre-condition checks. The do-while loop guarantees at least one execution, making it ideal for scenarios where the code needs to run before the condition is checked. By understanding their differences and use cases, you can choose the right loop for your needs and write efficient JavaScript code.

Live masterclass