Understanding JavaScript While Loop
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.
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
- The Syntax for the While Loop
- Do-while Loop
- Comparison of While and Do-while Loop
- Infinite While Loop
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.
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++;}
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++; }
v
Output:
Learner 1 Learner 2 Learner 3 Learner 4 Learner 5
Best-suited Programming courses for you
Learn Programming with these high-rated online courses
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);
Output:
Learner 0 Learner 1 Learner 2 Learner 3 Learner 4
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)
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.
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