Do While Loop in JavaScript

feature-14.jpg

Loops are important in any programming language. Loops in JavaScript are used to run a block of code again and again until a condition is met. JavaScript offers multiple types of loops, from which the while loop and the do-while loop are two commonly used loops. In this blog, we will learn all about the do-while loops in JavaScript, including examples, best practices, and the difference between the while and do-while loops in JavaScript.

Table of Contents:

What are Loops in JavaScript?

In JavaScript, a loop is defined as a tool that is used to run the same block of code again and again, as long as a certain condition is true. This is useful if you want to do something repeatedly, like printing values or checking values in the array. There are two types of loops in JavaScript:

  1. Entry Controlled Loops: Testing the condition is required before executing the body of the loop. The for loop and while loop are examples of entry-controlled loops in JavaScript.
  2. Exit Controlled Loops: Testing the condition is not required in exit-controlled loops. In the first iteration, the code is executed then the loop condition is checked. In simple words, the code written inside the loop body will be executed once, irrespective of whether the condition is true or false. The do…while loop is an example of an exit-controlled loop.

JavaScript While Loop

The JavaScript while loop is defined as a loop that is used when you want to repeat a certain block of code until a condition is true. It is found to be useful when you don’t know how many times you have to run the loop. The JavaScript while loop checks the condition before executing the code, and if the condition is true, then the code will be executed. It checks the condition again and executes the code until the loop condition is true.

JavaScript While Loop

Syntax:

while(condition) {
// body code
}

Example: Display the counting from 1 to 5 by using a JavaScript while loop.

Javascript

Output:

JavaScript while loop

Explanation: In this example, you are using a while loop to print the counting number from 1 to 5, and the variable i is declared with the value 1. The loop continuously checks for the value of i. If the value of i is greater than 5, then the loop stops and exits from the loop body.

Launch Your Web Development Career
Start Learning Now
quiz-icon

JavaScript do…while Loop

The do-while loop JavaScript is similar to the while loop, but the do-while loop runs the code at least once without checking the loop condition. The do-while loop JavaScript is used when you want your code to run at least one time without checking the condition.

JavaScript do...while Loop

Syntax:

do {
// code to run
} while (condition);

Example 1: Display the counting from 1 to 5 by using a do-while loop in JavaScript.

Javascript

Output:

do while loop javascript

Explanation: In this example, you are using a do-while loop in JavaScript to print the numbers from 1 to 5. Now this time, you’re using a do-while loop, so it prints 1 without checking the condition. After this, it will print the next numbers from 2 to 5.

Example 2: Using a false condition.

Javascript

Output:

JavaScript do while loop

Explanation: This example helps you to understand a do-while loop in JavaScript. Even though the condition (i<5) is false, the message “Hello, I am running once” is still printed because the do block runs once before checking the condition.

Best Practices for Using JavaScript do…while Loop

The do…while loop JavaScript is important if you want to run the code at least once without checking the condition. Here are some best practices that you need to follow while using the do-while loop in JavaScript:

  • The do-while loop JavaScript keeps the loop body code running as long as the condition is true. So it is important to update your loop variable.
  • It is good to use a do-while loop in JavaScript only when you run the code at least once, even if the condition is false.
  • The do-while loop is less commonly used than the for or while loop. Thus, it is good to add comments while using the do-while loop in JavaScript.
  • Try to keep the code simple so that other developers who see your code can easily understand the code context.

Difference Between while and do…while loop in JavaScript

Both the while and do-while loops in JavaScript are helpful in executing the same code multiple times. Here are some important differences between the while and do-while loops in JavaScript:

FeatureWhile LoopDo…while Loop
Control TypeIt is the entry-controlled loop, which means it checks the condition before running.It is an exit-controlled loop, which means it runs the code before checking the condition.
First ExecutionIt may not run the code if the condition is false.It always runs once, even if the condition is false.
Use CasesIt is used when you didn’t know how many times you had to run the loop.A do…while loop is used when you want to run the code at least once.
ReadabilityWhile loop offers more readability.It is less commonly used and requires adding comments for other developers.
Examplewhile(condition) {
// code
}
do {
// code
} while(condition);

Browser Compatibility

The do-while loop JavaScript is fully supported by all modern browsers. Here is the list of commonly used browsers that support the do-while loop JavaScript:

  • Google Chrome
  • Microsoft Edge
  • Mozilla Firefox
  • Apple Safari
  • Opera
  • Internet Explorer

Get 100% Hike!

Master Most in Demand Skills Now!

Conclusion

Loops are an important part of JavaScript. The JavaScript while loop is used when you want to run code as long as a condition is true. On the other hand, the do-while loop JavaScript runs code at least once without checking the condition. Understanding the do-while loop in JavaScript helps you to write clean and structured code.

To learn more about JavaScript Loops, check out this Web Development course and also explore JavaScript Interview Questions prepared by industry experts.

JavaScript do while loop – FAQs

Q1. What are loops in JavaScript?

Loops in JavaScript allow you to run the same block of code multiple times as long as the condition is true.

Q2. Why do we use a while loop in JavaScript?

The JavaScript while loop is used when you want to repeat the code again and again while a condition is true. If the condition is false at the start, the loop body will not be executed.

Q3. Why do we use a do-while loop in JavaScript?

The JavaScript do while loop is used when you want to run code at least once without checking the condition.

Q4. Why do we use a break in a while loop?

The break statement in JavaScript is used when the user want to stop the loop early. The loop breaks and stop executing code even if the condition is true.

Q5. How to skip loop iterations in JS?

You can use the continue statement to skip the current loop iteration.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner