The While loop
In JavaScript, a while loop is a control flow statement that allows a block of code to be repeatedly executed as long as a specified condition is true.
The syntax for a while loop in JavaScript is as follows:
javascriptCopy codewhile (condition) {
// Code block to be executed
}
NOTE: If the condition never becomes false, the loop will never end and this might crash the runtime.
do-while loop
In JavaScript, the do-while
loop is another type of loop that allows a block of code to be executed repeatedly as long as a specified condition is true. The main difference between a do-while
loop and a while
loop is that the do-while
loop guarantees that the code block is executed at least once, even if the condition is initially false.
The syntax for a do-while
loop in JavaScript is as follows:
javascriptCopy codedo {
// Code block to be executed
} while (condition);