English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript while statement

 JavaScript Statements and Variable Declarations

whileThe statement creates a loop that executes the specified statement as long as the test condition (condition) is true.

Only whenconditionbecomes false, the loop will stop.

Evaluate before executing the statementcondition.

JavaScript provides the following types of loops:

  • for -The loop traverses the code block several times

  • for...in-Traverse the properties of the object

  • while-The loop traverses the code block when the specified condition is true

  • do...while -The loop executes the code block once and then repeats the loop when the specified condition is true

UsedbreakThe statement terminates the current loop and usescontinueThe statement skips the value in the loop.

Syntax:

while (condition) {
//The statement is executed
}
var n = 0;
while (n < 5) {
   document.write("<br>The number is " + n);
   n++;
}
Test and See‹/›

Note:If you want to use aconditionInitialize the variable before the loop, and then increment it within the loop. If you forget to increase the variable, the loop will never end. This will cause your browser to crash.

Browser compatibility

All browsers fully support the while statement:

Statement
whileisisisisis

Parameter value

ParameterDescription
condition                The expression evaluated before each iteration of the loop. If the result of the condition is true, execute the statement. Execution will continue after the while loop when the result of the condition is false.    If the condition is always true, the loop will never end. This will cause your browser to crash.

Technical details

JavaScript version:ECMAScript 1

More examples

Loop through the array in ascending order:

var fruits = [;39;Apple;39;39;Mango;39;39;Banana;39;39;orange;39;];
var txt = ;39;39;;
var i = 0;
while (i < fruits.length) {
txt +fruits[i] + "<br>";
i++;
}
Test and See‹/›

Loop through the array in descending order:

var fruits = [;39;Apple;39;39;Mango;39;39;Banana;39;39;Orange;39;];
var txt = ;39;39;;
var i = fruits.length;
while (i != 0) {
i--;
txt +fruits[i] + "<br>";
}
Test and See‹/›

The following function has a break statement, when i is3Terminates the while loop and then returns the value3 * x:

function testBreak(x) {
  var i = 0;
  while (i < 6) {
if (i == 3) {
   break;
}
i++;
  }
  return i * x;
}
Test and See‹/›

The following example shows a while loop that has a continue statement, which is executed when the value of i is3Executes when:

var text = "";
var i = 0;
while (i < 6) {
   i++;
   if (i === 3) {
  continue;
   }
   text += "The number is" + i + "<br>";
}
Test and See‹/›

Related References

JavaScript Tutorial:JavaScript While Loop

JavaScript Reference:JavaScript do ... while Statement

JavaScript Reference:JavaScript for Statement

JavaScript Reference:JavaScript break Statement

JavaScript Reference:JavaScript continue Declaration

 JavaScript Statements and Variable Declarations