for

  • Revision slug: JavaScript/Reference/Statements/for
  • Revision title: for
  • Revision id: 64451
  • Created:
  • Creator: Tprogers
  • Is current revision? No
  • Comment /* Syntax */ changed "increment-expression" to "final-expression"

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.

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

Syntax

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

Parameters

initial-expression 
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.
condition 
An expression evaluated on each pass through the loop. If this condition evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true.
increment-expression 
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);
}

See also

while, do...while, for...in

{{ wiki.languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/for", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/for" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<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>
<table class="fullwidth-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>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval">for ([<i>initial-expression</i>]; [<i>condition</i>]; [<i>final-expression</i>])
   <i>statement</i>
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>initial-expression</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.
</dd></dl>
<dl><dt> <code>condition</code> </dt><dd> An expression evaluated on each pass through the loop. If this condition evaluates to true, <code>statement</code> is executed. This conditional test is optional. If omitted, the condition always evaluates to true.  
</dd></dl>
<dl><dt> <code>increment-expression</code> </dt><dd> Generally used to update or increment the counter variable.
</dd></dl>
<dl><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/Core_JavaScript_1.5_Reference/Statements/block">block</a> statement (<code>{ ... }</code>) to group those statements.
</dd></dl>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_for"> Example: Using <code>for</code> </h4>
<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="eval">for (var i = 0; i &lt; 9; i++) {
   n += i;
   myfunc(n);
}
</pre>
<h3 name="See_also"> See also </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Statements/while">while</a>,
<a href="en/Core_JavaScript_1.5_Reference/Statements/do...while">do...while</a>,
<a href="en/Core_JavaScript_1.5_Reference/Statements/for...in">for...in</a>
</p>
<div class="noinclude">
</div>
{{ wiki.languages( { "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/for", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/for" } ) }}
Revert to this revision