Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 417127 of while

  • Revision slug: Web/JavaScript/Reference/Statements/while
  • Revision title: while
  • Revision id: 417127
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment JavaScript/Reference/Statements/while Web/JavaScript/Reference/Statements/while

Revision Content

Summary

Creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.

Statement
Implemented in JavaScript 1.0
ECMAScript Edition ECMA-262

Syntax

while (condition) {
  statement
}

Parameters

condition
An expression evaluated before each pass through the loop. If this condition evaluates to true, statement is executed. When condition evaluates to false, execution continues with the statement after the while loop.
statement
A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements.

Examples

The following while loop iterates as long as n is less than three.

var n = 0;
var x = 0;

while (n < 3) {
  n++;
  x += n;
}

Each iteration, the loop increments n and adds it to x. Therefore, x and n take on the following values:

  • After the first pass: n = 1 and x = 1
  • After the second pass: n = 2 and x = 3
  • After the third pass: n = 3 and x = 6

After completing the third pass, the condition n < 3 is no longer true, so the loop terminates.

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Creates a loop that executes a specified statement as long as the test condition evaluates to true. The condition is evaluated before executing the statement.</p>
<table class="standard-table">
  <tbody>
    <tr>
      <td class="header" colspan="2">Statement</td>
    </tr>
    <tr>
      <td>Implemented in</td>
      <td>JavaScript 1.0</td>
    </tr>
    <tr>
      <td>ECMAScript Edition</td>
      <td>ECMA-262</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
while (<em>condition</em>) {
  <em>statement</em>
}</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>condition</code></dt>
  <dd>
    An expression evaluated before each pass through the loop. If this condition evaluates to true, <code>statement</code> is executed. When condition evaluates to false, execution continues with the statement after the <code>while</code> loop.</dd>
  <dt>
    <code>statement</code></dt>
  <dd>
    A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a <a href="/en-US/docs/JavaScript/Reference/Statements/block" title="JavaScript/Reference/Statements/block">block</a> statement (<code>{ ... }</code>) to group those statements.</dd>
</dl>
<h2 id="Examples" name="Examples">Examples</h2>
<p>The following <code>while</code> loop iterates as long as <code>n</code> is less than three.</p>
<pre class="brush:js">
var n = 0;
var x = 0;

while (n &lt; 3) {
  n++;
  x += n;
}</pre>
<p>Each iteration, the loop increments <code>n</code> and adds it to <code>x</code>. Therefore, <code>x</code> and <code>n</code> take on the following values:</p>
<ul>
  <li>After the first pass: <code>n</code> = 1 and <code>x</code> = 1</li>
  <li>After the second pass: <code>n</code> = 2 and <code>x</code> = 3</li>
  <li>After the third pass: <code>n</code> = 3 and <code>x</code> = 6</li>
</ul>
<p>After completing the third pass, the condition <code>n</code> &lt; 3 is no longer true, so the loop terminates.</p>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/do...while" title="JavaScript/Reference/Statements/do...while">do...while</a>, <a href="/en-US/docs/JavaScript/Reference/Statements/for" title="JavaScript/Reference/Statements/for">for</a></li>
</ul>
<!--languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/while" } )-->
Revert to this revision