JavaSript For Loop
Last Updated Jul 29, 2015, 07:00:14 PM
JavaSript For Loop
The for loop
statement creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a statement or a set of statements executed in the loop.
Syntax
initialization
An expression (including assignment expressions) or variable declaration. Typically used to initialize a counter variable. This expression may optionally declare new variables with the var keyword.
conditionAn expression to be evaluated before each loop iteration. If this expression evaluates to true, the statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.
final-expressionAn expression to be evaluated at the end of each loop iteration. It occurs before the next evaluation of the condition. The final expression is used to update or increment the counter variable.
statementA statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements. To execute no statement within the loop, use an empty statement (;).
Example
In the above example, the variable n initialized with 0, it checks if the variable n is less than 9, then it will print that variable while the value of n keeps increasing until the condition becomes false
Try It Now
Other JavaScript loop statements you might want to learn
JavaScript while loop
JavaScript do while loop
JavaScript break statement
JavaScript continue statement
Practice with out Interactive Editor and take your JavaScript Skills to the next level
Exercise 1 Exercise 2 Exercise 3 Exercise 4 Exercise 5 Exercise 6
Browser compatibility
Feature | |||||||
---|---|---|---|---|---|---|---|
For loop | Yes | Yes | Yes | Yes | Yes | Yes | Yes |
Sources and Credits
The source of the content has been referred and updated with Mozilla Developer Network and W3C Organization
Last Updated Jul 29, 2015, 07:00:14 PM
Complete Beginner to JavaScript Developer - Udemy
Advanced JavaScript Development - Udemy
Learn javascript in one month from experts -OneMonth
Become a Certified Web Developer - Udemy