JavaScript Kit > JavaScript Reference > JavaScript Statements > Here
JavaScript Statements- Looping
JavaScript Statements enable you to implement logic, looping, and more within your script.
Related Tutorials
- Understanding basic arrays and loops
- Variable and expression shortcuts
- The switch statement of JavaScript1.2
- JavaScript and OOP
- Creating custom objects in JavaScript
Looping Statements
Statements | Description |
---|---|
for |
Probably the most common looping statement, "for " executes the enclosed statements multiple times based on the 3 conditions an initial, limit, and increment condition) defined inside the for(...) parenthesis.
Syntax: for (initialValue; test; increment) Example: for (var x=0; x<3; x++){ You can define more than 1 expression for either the initial or increment conditions the for (var x=0, y=3; x<5; x++, y=x*3){ |
while |
A while loop continuously executes the enclosed statements as long as the tested expression evaluates to true. If the expression is false to begin with, the entire while loop is skipped.
Syntax: while (expression) Example: var number=0 |
do/while |
The do/while statement differs from the standard "while" in that the expression tested for is put at the end, ensuring that a do/while loop is always executed at least once..
Syntax: do Example: var number=0 |
for/in |
For/in is a special looping statement that lets you loop through the properties of an object, either built in or custom objects.
Syntax: for (var prop in object) where " For built in objects, Here are two examples. Example 1: This example writes out all the properties of the " for (var myprop in navigator){ Output:
Example 2: This example loops through all properties within a custom object: var userprofile={name:'George', age:30, sex:'male', getage:function(){return this.age}} Output:
Notice how the method |
break |
The break statement causes an exit of the innermost loop the statement is contained in. It's useful to prematurely end a loop:
Example: for (i=0; i<6; i++){ |
continue |
The continue statement causes JavaScript to skip the rest of the statements that may follow in a loop, and continue with the next iteration. The loop is not exited, unlike in a break statement.
Example: for (i=1; i<6; i++){ |
- JavaScript Operators
- JavaScript Statements
- Global functions
- JavaScript Events
- Escape Sequences
- Reserved Words