for statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement (usually a block statement ) to be executed in the loop.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
for ([initialization]; [condition]; [final-expression]) statement
initialization
var
or
let
keywords. Variables declared with
var
are not local to the loop, i.e. they are in the same scope the
for
loop is in. Variables declared with let are local to the statement.
The result of this expression is discarded.
condition
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
condition
. Generally used to update or increment the counter variable.
statement
{ ... }
) to group those statements. To execute no statement within the loop, use an
empty
statement (
;
).
for
下列
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 (let i = 0; i < 9; i++) {
console.log(i);
// more statements
}
for
expressions
All three expressions in the head of the
for
loop are optional.
For example, in the initialization block it is not required to initialize variables:
var i = 0;
for (; i < 9; i++) {
console.log(i);
// more statements
}
Like the initialization block, the condition block is also optional. If you are omitting this expression, you must make sure to break the loop in the body in order to not create an infinite loop.
for (let i = 0;; i++) {
console.log(i);
if (i > 3) break;
// more statements
}
You can also omit all three blocks. Again, make sure to use a
break
statement to end the loop and also modify (increase) a variable, so that the condition for the break statement is true at some point.
var i = 0;
for (;;) {
if (i > 3) break;
console.log(i);
i++;
}
for
without a statement
下列
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
section, a semicolon is used instead.
function showOffsetPos(sId) {
var nLeft = 0, nTop = 0;
for (
var oItNode = document.getElementById(sId); /* initialization */
oItNode; /* condition */
nLeft += oItNode.offsetLeft, nTop += oItNode.offsetTop, oItNode = oItNode.offsetParent /* final-expression */
); /* semicolon */
console.log('Offset position of \'' + sId + '\' element:\n left: ' + nLeft + 'px;\n top: ' + nTop + 'px;');
}
/* Example call: */
showOffsetPos('content');
// Output:
// "Offset position of "content" element:
// left: 0px;
// top: 153px;"
注意:
This is one of the few cases in JavaScript where
the semicolon is mandatory
. Indeed, without the semicolon the line that follows the cycle declaration will be considered a statement.
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'for statement' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
for
|
Chrome 1 | Edge 12 | Firefox 1 | IE 3 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs 0.1.100 |
完整支持