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 809309 of do...while

  • Revision slug: Web/JavaScript/Reference/Statements/do...while
  • Revision title: do...while
  • Revision id: 809309
  • Created:
  • Creator: m32po
  • Is current revision? No
  • Comment

Revision Content

{{jsSidebar("Statements")}}

The do...while statement creates a loop that executes a specified statement until the test condition evaluates to true. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.

Syntax

do
   statement
while (condition);
statement
A statement that is executed at least once and is re-executed each time the condition evaluates to true. To execute multiple statements within the loop, use a {{jsxref("Statements/block", "block")}} statement ({ ... }) to group those statements.
condition
An expression evaluated after each pass through the loop. If condition evaluates to true, the statement is re-executed. When condition evaluates to false, control passes to the statement following the do...while.

Examples

Using do...while

In the following example, the do...while loop iterates at least once and reiterates until i is no longer less than 5.

var i = 0;
do {
   i += 1;
   console.log(i);
} while (i < 5);

Specifications

Specification Status Comment
ECMAScript 3rd Edition. Standard Initial definition.
Implemented in JavaScript 1.2
{{SpecName('ES5.1', '#sec-12.6.1', 'do-while statement')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-do-while-statement', 'do-while statement')}} {{Spec2('ES6')}} Trailing ; is now optional.

Browser compatibility

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} IE6+ {{CompatVersionUnknown}} {{CompatVersionUnknown}}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}

See also

  • {{jsxref("Statements/while", "while")}}
  • {{jsxref("Statements/for", "for")}}

Revision Source

<div>{{jsSidebar("Statements")}}</div>

<p>The <strong><code>do...while</code> statement</strong> creates a loop that executes a specified statement until the test condition evaluates to true. The condition is evaluated after executing the statement, resulting in the specified statement executing at least once.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">
do
   <em>statement</em>
while (<em>condition</em>);
</pre>

<dl>
 <dt><code>statement</code></dt>
 <dd>A statement that is executed at least once and is re-executed each time the condition evaluates to true. To execute multiple statements within the loop, use a {{jsxref("Statements/block", "block")}} statement (<code>{ ... }</code>) to group those statements.</dd>
</dl>

<dl>
 <dt><code>condition</code></dt>
 <dd>An expression evaluated after each pass through the loop. If <code>condition</code> evaluates to true, the <code>statement</code> is re-executed. When <code>condition</code> evaluates to false, control passes to the statement following the <code>do...while</code>.</dd>
</dl>

<h2 id="Examples">Examples</h2>

<h3 id="Using_do...while">Using <code>do...while</code></h3>

<p>In the following example, the <code>do...while</code> loop iterates at least once and reiterates until <code>i</code> is no longer less than 5.</p>

<pre class="brush: js">
var i = 0;
do {
   i += 1;
   console.log(i);
} while (i &lt; 5);
</pre>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 3rd Edition.</td>
   <td>Standard</td>
   <td>Initial definition.<br />
    Implemented in JavaScript 1.2</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-12.6.1', 'do-while statement')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-do-while-statement', 'do-while statement')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>Trailing ; is now optional.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>IE6+</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatVersionUnknown}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{jsxref("Statements/while", "while")}}</li>
 <li>{{jsxref("Statements/for", "for")}}</li>
</ul>
Revert to this revision