The while loop


The while loop

The while loop is the most basic eloop in JavaScript, and it will be covered in this chapter. A while loop is used to execute a statement or code block repeatedly as long as an expression is true, and the loop ends when the expression becomes false.

The do...while Loop

It is a loop that allows you to do anything while you.'
The do...while loop is similar to the while loop, except that the condition check occurs at the end. This indicates that the loop will be executed at least once even if the condition is false. Don't forget to look for the semicolon after the do while loop.

Example No.1:

<html> <body> <h2>Knowledge2life</h2> <p id="demo"></p> <script> let text = ""; let i = 0; while (i < 5) { text += "<br>Knowledge2life " + i; i++; } document.getElementById("demo").innerHTML = text; </script> </body> </html>

OUTPUT:

Knowledge2life

Knowledge2life 0
Knowledge2life 1
Knowledge2life 2
Knowledge2life 3
Knowledge2life 4