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 417091 of for

  • Revision slug: Web/JavaScript/Reference/Statements/for
  • Revision title: for
  • Revision id: 417091
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Version information header is required to stop the table showing on https://developer.mozilla.org/en-US/docs/JavaScript/Reference/StatementsJavaScript/Reference/Statements/for Web/JavaScript/Reference/Statements/for

Revision Content

Summary

Creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement executed in the loop.

Version Information

Statement
Implemented in: JavaScript 1.0, NES 2.0
ECMA Version: ECMA-262

Syntax

for ([initialization]; [condition]; [final-expression])
   statement

Parameters

initialization
An expression (including assignment expressions) or variable declaration. Typically used to initialize a counter variable. This expression may optionally declare new variables with the var keyword. These variables are not local to the loop, i.e. they are in the same scope the for loop is in. The result of this expression is discarded.
condition
An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.
final-expression
An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of condition. Generally used to update or increment the counter variable.
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

Example: Using for

The following for statement starts by declaring the variable i and initializing it to 0. It checks that i is less than nine, performs the two succeeding statements, and increments i by 1 after each pass through the loop.

for (var i = 0; i < 9; i++) {
   n += i;
   myfunc(n);
}

Example: Using for without the statement section

The following for cycle calculates the offset position of a node in the [final-expression] section, and therefore it does not require the use of a statement or block statement section:

function showOffsetPos (sId) {
  var nLeft = 0, nTop = 0;

  for (var oItNode = document.getElementById(sId); oItNode; nLeft += oItNode.offsetLeft, nTop += oItNode.offsetTop, oItNode = oItNode.offsetParent);
  alert("Offset position of \"" + sId + "\" element:\n left: " + nLeft + "px;\n top: " + nTop + "px;");
}
Note: In this case, when you do not use the statement section, a semicolon is put immediately after the declaration of the cycle.

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement executed in the loop.</p>
<h2 id="Version_Information">Version Information</h2>
<table class="standard-table">
  <tbody>
    <tr>
      <td class="header" colspan="2">Statement</td>
    </tr>
    <tr>
      <td>Implemented in:</td>
      <td>JavaScript 1.0, NES 2.0</td>
    </tr>
    <tr>
      <td>ECMA Version:</td>
      <td>ECMA-262</td>
    </tr>
  </tbody>
</table>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
for ([<em>initialization</em>]; [<em>condition</em>]; [<em>final-expression</em>])
   <em>statement</em>
</pre>
<h3 id="Parameters" name="Parameters">Parameters</h3>
<dl>
  <dt>
    <code>initialization</code></dt>
  <dd>
    An expression (including assignment expressions) or variable declaration. Typically used to initialize a counter variable. This expression may optionally declare new variables with the <code>var</code> keyword. These variables are not local to the loop, i.e. they are in the same scope the <code>for</code> loop is in. The result of this expression is discarded.</dd>
  <dt>
    <code>condition</code></dt>
  <dd>
    An expression to be evaluated before each loop iteration. If this expression evaluates to true, <code>statement</code> is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the <code>for</code> construct.</dd>
  <dt>
    <code>final-expression</code></dt>
  <dd>
    An expression to be evaluated at the end of each loop iteration. This occurs before the next evaluation of <code>condition</code>. Generally used to update or increment the counter variable.</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>
<h3 id="Example.3A_Using_for" name="Example.3A_Using_for">Example: Using <code>for</code></h3>
<p>The following <code>for</code> statement starts by declaring the variable <code>i</code> and initializing it to <code>0</code>. It checks that <code>i</code> is less than nine, performs the two succeeding statements, and increments <code>i</code> by 1 after each pass through the loop.</p>
<pre class="brush: js">
for (var i = 0; i &lt; 9; i++) {
   n += i;
   myfunc(n);
}
</pre>
<h3 id="Example.3A_Using_for_without_the_statement_section" name="Example.3A_Using_for_without_the_statement_section">Example: Using <code>for</code> without the <code>statement</code> section</h3>
<p>The following <code>for</code> cycle calculates the offset position of a node in the <em>[final-expression]</em> section, and therefore it does not require the use of a <code>statement</code> or <code><a href="/en-US/docs/JavaScript/Reference/Statements/block" title="JavaScript/Reference/Statements/block">block</a> statement</code> section:</p>
<pre class="brush: js">
function showOffsetPos (sId) {
  var nLeft = 0, nTop = 0;

  for (var oItNode = document.getElementById(sId); oItNode; nLeft += oItNode.offsetLeft, nTop += oItNode.offsetTop, oItNode = oItNode.offsetParent);
  alert("Offset position of \"" + sId + "\" element:\n left: " + nLeft + "px;\n top: " + nTop + "px;");
}</pre>
<div class="note">
  <strong>Note:</strong> In this case, when you do not use the <code>statement</code> section, <strong>a semicolon is put immediately after the declaration of the cycle</strong>.</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/break" title="JavaScript/Reference/Statements/break">break</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/continue">continue</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/while" title="JavaScript/Reference/Statements/while">while</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/do...while" title="JavaScript/Reference/Statements/do...while">do...while</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/for...in" title="JavaScript/Reference/Statements/for...in">for...in</a></li>
  <li><a href="/en-US/docs/JavaScript/Reference/Statements/for...of" title="JavaScript/Reference/Statements/for...of">for...of</a></li>
</ul>
Revert to this revision