Skip to content

Latest commit

 

History

History
99 lines (79 loc) · 3.13 KB

File metadata and controls

99 lines (79 loc) · 3.13 KB
title ms.custom ms.date ms.prod ms.reviewer ms.suite ms.technology ms.tgt_pltfrm ms.topic f1_keywords dev_langs helpviewer_keywords ms.assetid caps.latest.revision author ms.author manager
for Statement (JavaScript) | Microsoft Docs
01/18/2017
windows-client-threshold
devlang-javascript
language-reference
for_JavaScriptKeyword
JavaScript
TypeScript
DHTML
loop structures, for statements
bae0ec40-152e-43f3-969b-3696489ec5c4
16
mikejo5000
mikejo
ghogen

for Statement (JavaScript)

Executes a block of statements for as long as a specified condition is true.

Syntax

for ([initialization]; [test]; [increment])  
   statement   

Parameters

initialization
Optional. An expression. This expression is executed only once, before the loop is executed.

test
Optional. A Boolean expression. If test is true, statement is executed. If test if false, the loop is terminated.

increment
Optional. An expression. The increment expression is executed at the end of every pass through the loop.

statement
Optional. One or more statements to be executed if test is true. Can be a compound statement.

Remarks

You usually use a for loop when the loop is to be executed a known number of times. A for loop is useful for iterating over arrays and for performing sequential processing.

The test of a conditional expression occurs before the execution of the loop, so a for statement executes zero or more times.

On any line in a for loop statement block, you can use the break statement to exit the loop, or you can use the continue statement to transfer control to the next iteration of the loop.

Example

In the following example, the for statement executes the enclosed statements as follows:

  • First, the initial value of the variable i is evaluated.

  • Then, as long as the value of i is less than or equal to 9, the document.write statements are executed and i is reevaluated.

  • When i is greater than 9, the condition becomes false and control is transferred outside the loop.

// i is set to 0 at the start and is incremented by 1 at the  
// end of each iteration.  
// The loop terminates when i is not less than or equal to  
// 9 before a loop iteration.  
for (var i = 0; i <= 9; i++) {  
   document.write (i);  
   document.write (" ");  
}  
  
// Output: 0 1 2 3 4 5 6 7 8 9  

Example

All of the expressions of the for statement are optional. In the following example, the for statements create an infinite loop, and a break statement is used to exit the loop.

var j = 0;  
for (;;) {  
    if (j >= 5) {  
        break;  
    }  
    j++;  
    document.write (j + " ");  
}  
  
// Output: 1 2 3 4 5  

Requirements

[!INCLUDEjsv1]

See Also

for...in Statement
while Statement