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.
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.