YouTip LogoYouTip

Jsref For

Here's the translated HTML content with all code blocks preserved: [![Image 1: JavaScript Statements Reference](#) JavaScript Statements Reference](#) ## Examples Loop through a code block 5 times: var text = ""; var i; for (i = 0; i < 5; i++) { text += "The number is " + i + "
"; } _text_ output result: The number is 0 The number is 1 The number is 2 The number is 3 The number is 4 [Try it Β»](#) More examples at the bottom of this page. * * * ## Definition and Usage The for statement will continue to execute as long as the condition evaluates to true. The loop will keep running as long as the condition remains true, stopping only when the condition becomes false. The for loop is a tool you'll frequently use when you want to create loops. JavaScript supports different types of loops: * **for** - loops through a block of code a number of times * ****[for/in](#)**** - loops through the properties of an object * **(#)** - loops through a block of code while a specified condition is true * **[do/while](#)** - also loops through a block of code while a specified condition is true **Tip:** Use the (#) statement to break out of a loop, and the (#) statement to skip the current iteration and proceed to 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 Values | Parameter | Description | | --- | --- | | _statement1_ | Optional. Executed before the loop starts, used to initialize variables. Multiple variables can be initialized using commas (,) as separators. **Note:** This parameter can be omitted. However, don't omit the semicolon ";". | | _statement2_ | Optional. Defines the condition for executing the loop. Typically used for conditional evaluation - if true, the loop continues; if false, the loop stops. **Note:** This parameter can be omitted. However, don't omit the semicolon ";". Also, if you omit this parameter, you must provide a break condition within the loop. Otherwise, the statement will loop infinitely and crash the browser. | | _statement3_ | Optional. Executed after each loop iteration. Typically used to increment or decrement counter variables. **Note:** This parameter can be omitted (e.g., when increment/decrement operations are performed inside the loop). | ## Technical Details | JavaScript Version: | 1.0 | | --- | * * * ![Image 2: Examples](#) ## More Examples ## Example Print car names by looping through array indices: var cars = ["BMW", "Volvo", "Saab", "Ford"]; var text = ""; var i; for (i = 0; i < cars.length; i++) { text += cars + "
"; } [Try it Β»](#) Explanation of the above example: * First, we set a variable before the loop starts (var i = 0;) * Then, we define the loop execution condition. The loop continues as long as the variable value is less than the array length (which is 4) * Each time the loop executes, the variable increments by 1 (i++) * When the variable is no longer less than 4 (array length), the condition becomes false and the loop ends. ## 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 + "
"; } [Try it Β»](#) ## Example Omit the first parameter (set values before the loop): var cars = ["BMW", "Volvo", "Saab", "Ford"]; var i = 2; var len = cars.length; var text = ""; for (; i < len; i++) { text += cars + "
"; } [Try it Β»](#) ## Example Use continue statement - loop through a code block but skip the iteration when 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 break statement - loop through a code block but exit when 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 use the **(#)** statement to exit the loop when i equals "3" (if the second parameter is omitted, you must set a break condition inside the loop. Otherwise, the loop won't terminate and may crash the browser): var cars = ["BMW", "Volvo", "Saab", "Ford"]; var text = ""; var i; for (i = 0; ; i++) { if (i == 3) { break; } text += cars + "
"; } [Try it Β»](#) ## Example Loop through array indices in descending order: var cars = ["BMW", "Volvo", "Saab", "Ford"]; var text = ""; var i; for (i = cars.length - 1; i >= 0; i--) { text += cars + "
"; } [Try it Β»](#) ## Example Omit the last parameter and increment the value inside the loop: var cars = ["BMW", "Volvo", "Saab", "Ford"]; var i = 0; var len = cars.length; for (; i < len;) { text += cars + "
"; i++; } [Try it Β»](#) ## Example Loop through NodeList objects and change the background color of all

elements in the list: var myNodelist = document.getElementsByTagName("P"); var i; for (i = 0; i < myNodelist.length; i++) { myNodelist.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 Reference: [JavaScript for ... in Statement](#) JavaScript Reference: (#) JavaScript Reference: (#) JavaScript Reference: (#) * * JavaScript Statements Reference](#)

← Jsref ForinJsref Dowhile β†’