Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript for Statement

JavaScript for Statement

JavaScript Statement Reference JavaScript statement reference manual

Example

Loop the code block 5 times:

var text = "";

var i;

for (i = 0; i < 5; i++) {

    text += "The number is " + i + "
";

}

text The output is:

The number is 0

The number is 1

The number is 2

The number is 3

The number is 4

Try it »

The bottom of this article contains more examples.


Definition and Usage

The for statement keeps executing as long as the condition is true.

As long as the condition is true, the loop continues to execute until the condition becomes false.

The for loop is a tool you will often use when you want to create a loop.

JavaScript supports different types of loops:

Tip: Use break statement to break out of the loop, usecontinue statement to skip the current iteration and execute the next one.


Browser support

Statement
for Yes Yes Yes Yes Yes

Syntax

for (statement 1; statement 2; statement 3) {
    code block to be executed
}

Parameter value

Parameters Description
statement1 Optional. Executed before the loop, used to initialize variables. Use commas (,) to initialize multiple variables.
 ** 注意:** 该参数可以省略。但是,不要忽略 分号 ";"。 |

| statement2 | Optional. Defines the condition for executing the loop. Usually this statement is used for condition checking; if the condition is true the loop continues, and if false the loop terminates.

 **注意:** 该参数可以省略。但是,不要忽略 分号 ";"。同样,如果你忽略了该参数,你必须在循环中提供跳出循环的条件。否则语句无限循环,会造成浏览器崩溃。 |

| statement3 | Optional. Executed after each iteration of the loop. Usually this statement is used to increment or decrement the counter variable.

 **注意:** 该参数可以忽略(例如: 自增或自减值在循环内操作) |

Technical Details

JavaScript version: 1.0

More examples

Example

Print the car names by looping through the array index:

var cars = ["BMW", "Volvo", "Saab", "Ford"];

var text = "";

var i;

for (i = 0; i < cars.length; i++) {

    text += cars[i] + "
";

}

Try it »

Analysis of the example above:

Example

Initialize multiple values in the first parameter:

var cars = ["BMW", "Volvo", "Saab", "Ford"];

var i;

for (i = 0, len = cars.length, text = ""; i < len; i++) {

    text += cars[i] + "
";

}

Try it »

Example

Omit the first parameter (set the value before the loop):

var cars = ["BMW", "Volvo", "Saab", "Ford"];

var i = 2;

var len = cars.length;

var text = "";

for (; i < len; i++) {

    text += cars[i] + "
";

}

Try it »

Example

Use the continue statement - loop the code block, but skip the iteration where i equals "3":

var text = ""

var i;

for (i = 0; i < 5; i++) {

    if (i == 3) {

        continue;

    }

    text += "The number is " + i + "
";

}

Try it »

Example

Use the break statement - loop the code block, but exit the loop when the variable i equals "3":

var text = ""

var i;

for (i = 0; i < 5; i++) {

    if (i == 3) {

        break;

    }

    text += "The number is " + i + "
";

}

Try it »

Example:

Omit the second parameter. In this example, we also used the break statement to exit the loop when i equals "3" (if the second parameter is omitted, you must set the loop exit condition inside the loop body; otherwise the loop cannot terminate and will crash the browser):

var cars = ["BMW", "Volvo", "Saab", "Ford"];

var text = "";

var i;

for (i = 0; ; i++) {

    if (i == 3) {

        break;

    }

    text += cars[i] + "
";

}

Try it »

Example

Loop through the array index in descending order:

var cars = ["BMW", "Volvo", "Saab", "Ford"];

var text = "";

var i;

for (i = cars.length - 1; i >= 0; i--) {

    text += cars[i] + "
";

}

Try it »

Example

Omit the last parameter and increment the value inside the loop body:

var cars = ["BMW", "Volvo", "Saab", "Ford"];

var i = 0;

var len = cars.length;

for (; i < len;) {

    text += cars[i] + "
";

    i++;

}

Try it »

Example

Loop through the nodes of the NodeList object and modify all the

elements' background color:

var myNodelist = document.getElementsByTagName("P");

var i;

for (i = 0; i < myNodelist.length; i++) {

    myNodelist[i].style.backgroundColor = "red";

}

Try it »

Example

Nested loop example:

var text = "";

var i, j;

for (i = 0; i < 3; i++) {

    text += "
" + "i = " + i + ", j = ";

    for (j = 10; j < 15; j++) {

        document.getElementById("demo").innerHTML = text += j + " ";

    }

}

Try it »


Related pages

JavaScript tutorial: JavaScript For Loop

JavaScript Reference Manual: JavaScript for ... in Statement

JavaScript Reference Manual: JavaScript break statement

JavaScript Reference Manual: JavaScript continue Statement

JavaScript Reference Manual: JavaScript while Statement


JavaScript Statement Reference JavaScript statement reference manual