The while loop in JavaScript
While writing a program, you may encounter a situation where you need to perform an iterative action. In these situations, you will need to write loop commands to reduce the number of lines of code.
JavaScript supports all the necessary loops to reduce the pressure of the program:
While loop
The most basic loop in JavaScript is the while loop that will be discussed in this chapter. The purpose of the while loop is to execute a command or repeated code block as long as the expression - the expression is true . Once the expression becomes false , the loop ends.
Implementation diagram
The diagram showing the while loop implementation process is as follows:

Syntax
The following is the while loop syntax in JavaScript:
while ( expression ){ Statement ( s ) to be executed if expression is true }
For example
You try the following example to execute a while loop.
Set the variable to different value and then try.
Result:
Starting Loop
Current Count: 0
Current Count: 1
Current Count: 2
Current Count: 3
Current Count: 4
Current Count: 5
Current Count: 6
Current Count: 7
Current Count: 8
Current Count: 9
Loop stopped!
Đặt biến vào giá trị khác nhau và thử thử .
Do . while loop
The do . while loop is similar to the while loop except that the condition check occurs at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false .
Implementation diagram
Here is the do-while loop implementation diagram:
Syntax
Do . while loop syntax in JavaScript is as follows:
do { Statement ( s ) to be executed ; } while ( expression );
Note - Don't forget the semicolon used at the end of the do . while loop.
For example
Try the following example to learn how to execute a do . while loop in JavaScript.
Set the variable to different value and then try.
Result:
Starting Loop
Current Count: 0
Current Count: 1
Current Count: 2
Current Count: 3
Current Count: 4
Loop Stopped!
Đặt biến vào giá trị khác nhau và thử thử .
Follow tutorialspoint
Previous article: Switch Case command in JavaScript
Next article: For loop in JavaScript
You should read it
Maybe you are interested
Instructions for creating text using AI on Notepad Windows 11 Best Anime Like My Dress Up Darling What is Large Action Model (LAM)? SpaceX's Crew Dragon spacecraft successfully assembled with the ISS station, completely automated Will NASA partner with Tesla to create the most unprecedented lunar exploration vehicle? SpaceX is about to blow up a tens of millions of dollars worth of boosters on the Atlantic