Latest web development tutorials

JavaScript for statements

JavaScript Statements Reference Manual JavaScript Statements Reference Manual

Examples

Loop code blocks 5 times:

var text = "";
var i;
for (i = 0; i <5; i ++) {
text + = "The number is" + i + "<br>";
}

text output is:

The number is 0
The number is 1
The number is 2
The number is 3
The number is 4

try it"

Bottom of this article contains more examples.


Definition and Usage

for as long as the judgment condition statement is true it would have been executed.

As long as the condition is true, there is continuous loop execution until the condition is false.

for loop is that you want to create a loop often used tool.

JavaScript support different types of loops:

  • for - a certain number of cycles code block
  • for / in - loop through the properties of an object
  • the while - when a specified condition is true cycle specified block of code
  • do / the while - the same as the specified condition is true cycle specified block of code

Tip: Use the break statement to jump out of circulation, use continue statement to skip the iteration point before the next iteration and execution.


Browser Support

Statements
for Yes Yes Yes Yes Yes


grammar

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

Parameter Value

parameter description
statement1 Optional. Before the cycle execution for variable initialization, initialize multiple variables separated by commas (,).

Note: This parameter can be omitted. However, do not ignore the semicolon ";."
statement2 Optional. Define the execution condition for the loop. This statement is usually used for condition judgment, if the condition is true, the loop will continue, if false, the loop terminates.

Note: This parameter can be omitted. However, do not ignore the semicolon ";." Similarly, if you omit this parameter, you must provide the conditions out of the cycle of the loop. Otherwise infinite loop statement will cause the browser to crash.
statement3 Optional. After each execution of the loop statement. Typically increment or decrement the count variable statement.

Note: This parameter can be ignored (for example: incrementing or impaired operation within the loop)

technical details

JavaScript version: 1.0


Examples

More examples

Examples

Print the name of the bus loop through the array index:

var cars = [ "BMW", "Volvo", "Saab", "Ford"];
var text = "";
var i;
for (i = 0; i <cars.length; i ++) {
text + = cars [i] + "<br>";
}

try it"

Examples of the above analysis:

  • First, we set the variable before the start of the cycle (var i = 0;)
  • Then, we define the conditions for execution of the loop. Loop will always perform until the variable value is less than the length of the array (of length 4)
  • Each time through the loop, the variable will increment 1 (i ++)
  • If the variable is no longer than 4 (length of the array), the condition is false, exit the loop.

Examples

: The first argument to initialize multiple values

var cars = [ "BMW", "Volvo", "Saab", "Ford"];
var i;
for (i = 0, len = cars.length, text = ""; i <len; i ++) {
text + = cars [i] + "<br>";
}

try it"

Examples

Ignore the first parameter (loop before setting the value):

var cars = [ "BMW", "Volvo", "Saab", "Ford"];
var i = 2;
var len = cars.length;
var text = "";
for (; i <len; i ++) {
text + = cars [i] + "<br>";
}

try it"

Examples

Use continue statement - loop code block, but skip i is equal to "3" cycle:

var text = ""
var i;
for (i = 0; i <5; i ++) {
if (i == 3) {
continue;
}
text + = "The number is" + i + "<br>";
}

try it"

Examples

Use the break statement - loop code block, but exits the loop variable i is equal to "3":

var text = ""
var i;
for (i = 0; i <5; i ++) {
if (i == 3) {
break;
}
text + = "The number is" + i + "<br>";
}

try it"

Example:

Ignore the second parameter. In this example, we also use the break statement to exit the loop i is equal to "3" (If the second argument is ignored, you have to set the conditions out of the loop in the loop body Otherwise, the cycle can not be terminated, it will cause the browser to crash.):

var cars = [ "BMW", "Volvo", "Saab", "Ford"];
var text = "";
var i;
for (i = 0;; i ++) {
if (i == 3) {
break;
}
text + = cars [i] + "<br>";
}

try it"

Examples

Loop array index in descending manner:

var cars = [ "BMW", "Volvo", "Saab", "Ford"];
var text = "";
var i;
for (i = cars.length - 1; i> = 0; i--) {
text + = cars [i] + "<br>";
}

try it"

Examples

The last parameter is ignored, the body of the loop increment value:

var cars = [ "BMW", "Volvo", "Saab", "Ford"];
var i = 0;
var len = cars.length;
for (; i <len;) {
text + = cars [i] + "<br>";
i ++;
}

try it"

Examples

NodeList object node cycle and modify the background color of the list of all the <p> element:

var myNodelist = document.getElementsByTagName ( "P");
var i;
for (i = 0; i <myNodelist.length; i ++) {
myNodelist [i] .style.backgroundColor = "red";
}

try it"

Examples

Examples of nested loops:

var text = "";
var i, j;

for (i = 0; i <3; i ++) {
text + = "<br>" + "i =" + i + ", j =";

for (j = 10; j <15; j ++) {
. Document.getElementById ( "demo") innerHTML = text + = j + "";
}
}

try it"


Related Pages

JavaScript Tutorials: JavaScript the 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 the while statement


JavaScript Statements Reference Manual JavaScript Statements Reference Manual