Understanding JavaScript While Loop

Understanding JavaScript While Loop

4 mins read351 Views Comment
Updated on Dec 27, 2023 15:44 IST

The JavaScript while loop continuously executes a block of code as long as a specified condition is true, offering a way to repeat operations efficiently until a certain criterion is met. It's particularly useful for tasks where the number of iterations isn’t predetermined at the start of the loop.

2022_11_While-Loop.jpg

Loops can run a chunk of code repeatedly. Loops in JavaScript are used when the user wants to run the same code, as loops come in handy repeatedly. The while loop is one of the most important loops in JavaScript.

With the help of demonstrations, you will learn about the while loop and the do-while loop in this article.

Explore: Online JavaScript Courses and Certifications

Table of Content

While Loop In JavaScript

According to the specified boolean condition, the code can be executed continuously using a while loop in Javascript. The while loop can be viewed as an iterative version of the if statement.

The loop can repeatedly run a given code block until the condition isn’t met.

The two major types of loops are

  • Loops with controlled entry: In this kind of loop, the test state is examined before accessing the loop body. Entry-controlled loops include the For Loop and the While Loop.
  • Exit Controlled Loops: The test state is evaluated after the loop body in this sort of loop. Therefore, the loop body will run at least once, whether the test state is true or false. Do-while loops are loops with exit controls.

JavaScript Object Methods- How to Use Them?
JavaScript Object Methods- How to Use Them?
JavaScript methods are actions that can be executed on objects. This article will discuss JavaScript method objects, types of JavaScript methods, and their features. You will learn about its various...read more

JavaScript Uses in Various Sectors
JavaScript Uses in Various Sectors
Many websites utilize the lightweight, object-oriented programming language JavaScript to write their web pages. It is a complete programming language that is interpreted. When used with HTML, JavaScript allows dynamic...read more

The Syntax for the While Loop

while (condition) {    // Statements}

Let us see a JavaScript code below to understand a basic program that displays numbers from 1 to 6.


 
let num = 1;
while (num <= 6)
{
console.log(num);
num++;
}
Copy code

Output:

123456

Let us see another example to have a better understanding of the while loop in JavaScript:


 
var count = 1;
while(count < 6)
{
console.log("Learner " + count);
count++;
}
Copy code
v
Copy code

Output:

Learner 1
Learner 2
Learner 3
Learner 4
Learner 5
Recommended online courses

Best-suited Programming courses for you

Learn Programming with these high-rated online courses

3.02 L
36 months
3.15 L
4 years
Free
6 weeks
7.1 K
6 weeks
Free
10 hours
Free
8 weeks
1.24 L
48 months
Free
15 weeks

Do-while Loop

A control flow statement known as a “do-while loop” continually runs a block of code, or does not, based on a given boolean value at the conclusion of the block.

The syntax of the do-while loop is:

do {    // Statement}while (condition);

Let us see an example to have a better understanding of the do-while loop in JavaScript:


 
var count = 0;
do {
console.log("Learner " + count);
count++;
}
while (count < 5);
Copy code

 

Output:
Learner 0
Learner 1
Learner 2
Learner 3
Learner 4

JavaScript Array – How to Use Them?
JavaScript Array – How to Use Them?
JavaScript arrays are versatile, ordered collections of elements that can hold items of any data type, including numbers, strings, or objects. They offer a range of methods for traversal, manipulation,...read more

JavaScript Functions
JavaScript Functions
A JavaScript function is a block of reusable code designed to perform a particular task, enhancing code efficiency and organization. Functions are invoked or called to execute their defined operations...read more

Comparison of While and Do-while Loop

Before testing the while loop’s condition, the do-while loop runs the loop’s code once. Before executing the content, the while loop will first check the condition.

While Loop Do-while Loop
It is a looping structure with entry conditions.
The condition provided in the while block determines the number of iterations.
It is a looping structure with an exit condition.
There will be at least one repetition regardless of the condition stated in the do-while block.
The loop’s starting point has access to the block control condition. The loop’s terminus contains the block control condition.

Infinite While Loop

If a loop’s condition is always true, it will loop indefinitely (until the memory is full).

while(true){

    // Statements
}

Let us see a very simple demonstration of the infinite do-while loop.


 
const count = 1; do { // Statements } while(count == 1)
Copy code

For the programs mentioned above, the condition is always true. The body of the loop will therefore run indefinitely.

Check out: Top 85+ JavaScript Interview Questions and Answers for 2022

Conclusion

In this article, we have discussed the ‘while’ loop in JavaScript.  The while loop can be viewed as an iterative version of the if statement. The loop can repeatedly run a given block of code until the condition isn’t met. The two major types of loops are entry-controlled and exit-controlled. We have also talked about the do-while loop along with its examples and infinite while loop. 

Contributed by: Megha Chadha

Top FAQs on While Loop in JavaScript

What is a while loop in JavaScript?

A while loop in JavaScript repeatedly executes a block of code as long as a specified condition is true, making it ideal for scenarios where the number of iterations is not known beforehand.

How do you write a while loop in JavaScript?

A while loop is written with the while keyword, followed by a condition in parentheses and a block of code in curly braces. For example: while (condition) { /* code to execute */ }.

What is an infinite loop and how can it occur in a while loop?

An infinite loop occurs when the loop's condition never becomes false. In a while loop, this can happen if the condition is always true and there’s no code within the loop to break or modify it.

How do you avoid infinite loops in while loops?

Ensure the loop's condition will eventually become false. This often involves modifying a variable within the loop that affects the condition, or using a break statement to exit the loop based on certain criteria.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio