English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JavaScript Statements and Variable Declarations
forA statement creates a loop that executes the specified statement as long as the calculation result of the condition (condition) is true.
Only whenCondition (condition)The loop will stop only when it becomes false.
JavaScript provides the following types of loops:
for -Traverse the code block several times in a loop
for...in-Traverse the properties of an object
while-When the specified condition is true, the code block is traversed in a loop
do...while -do...while
To execute the code block once and then repeat the loop when the specified condition is truebreakStatement terminates the current loop and usescontinueStatement skips the value in the loop.
for (initialization; condition; final-expression) { //The statement to be executed }
for (let n = 0; n < 5; n++) { document.write("<br>The number is " + n); }Test and See‹/›
All browsers fully support the for statement:
Statement | |||||
for | is | is | is | is | is |
Parameters | Description |
---|---|
initialization | (Optional) Execute before the loop starts. Usually, this statement is used to initialize the counter variable. To start multiple values, separate each value with a comma. |
condition | (Optional) Define the condition for running the loop. Usually, this statement is used to evaluate the condition of the counter variable. If it returns true, the loop will restart; if it returns false, the loop will end. Note: If omitted, the condition is always true. This will cause your browser to crash. |
final-expression | (Optional) Execute after each loop iteration. Usually, this statement is used to increment or decrement a counter variable. |
JavaScript version: | ECMAScript 1 |
---|
Loop through the array in ascending order:
var fruits = ["39;Apple', &39;Mango', &39;Banana', &39;Orange']; var txt = ''; for (var i = 0; i < fruits.length;++) { "; +fruits[i] + "<br>"; }Test and See‹/›
Loop through the array in descending order:
var fruits = ["39;Apple', &39;Mango', &39;Banana', &39;Orange']; var txt = ''; for (var i = fruits.length -1; i >= 0; i--) { "; +fruits[i] + "<br>"; }Test and See‹/›
Loop through the nodes of the NodeList object and change the background color of all elements in the list:
var x = document.querySelectorAll(".demo"); for (let i = 0; i < x.length;++) { x[i].style.backgroundColor = "coral"; }Test and See‹/›
Nested loops are used most places, they are used in matrix multiplication, displaying tables, and many other places:
var txt = ""; for (var row = 0; row < 10; row++) { ; row++) { "; +for (var col = 0; col < row; * = "" } "; +txt }Test and See‹/›
= "<br>";3The following function has a break statement, which terminates when i is
var text = ""; for (let i = 0; i < 6; i++) { if (i === 3) { break; } text += "The number is" + i + "<br>"; }Test and See‹/›
The following example shows a for loop with a continue statement, which terminates the for loop when the value of i is3Execute when:
var text = ""; for (let i = 0; i < 6; i++) { if (i === 3) { continue; } text += "The number is" + i + "<br>"; }Test and See‹/›
JavaScript Reference:JavaScript for...in Statement
JavaScript Reference:JavaScript while Statement
JavaScript Reference:JavaScript break Statement
JavaScript Reference:JavaScript continue Statement