while
loop in JavaScript works same like for loop in JavaScript but in a little bit different manner. while loop runs until the condition is true and exits when the condition is false.
Syntax
while (condition) { // statements to be executed here; }
condition: expression that is to evaluated that decides whether to execute the next iteration of loop. If true, the loop is executed otherwise it terminates the loop.
statements: the code statements that are executed for each condition evaluated to true.
Example
var i=1; while(i<=7) { document.write('<font size=\"' + i + '\">Hello World</font><br />'); i++; }
Above example shows the use of while
loop in JavaScript by executing a single line of code how you can increase the size of font in each loop by incrementing the value of variable i
.