Categories:

JavaScript Kit > JavaScript Reference > JavaScript Statements > Here

JavaScript Statements- Looping

Last updated: August 29th, 2008

JavaScript Statements enable you to implement logic, looping, and more within your script.

Related Tutorials

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)
statement

Example:

for (var x=0; x<3; x++){
 document.write("This text is repeated three times<br>")
}

//or simply:

for (var x=0; x<3; x++)
 document.write("This text is repeated three times<br>")

//for single line for statement

You can define more than 1 expression for either the initial or increment conditions the for loop uses to decide whether to continue. Simply separate each expression with a comma (,). For example:

for (var x=0, y=3; x<5; x++, y=x*3){
 document.write("x:"+x+" ")
 document.write("y:"+y+" ")
}

//outputs: x:0 y:3 x:1 y:3 x:2 y:6 x:3 y:9 x:4 y:12

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)
 statement

Example:

var number=0
while (number<5){
 document.write(number+"<br>")
 number++
}

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
 statement
while (expression)

Example:

var number=0
do{
 document.write(number+"<br>")
 number++
}
while (number<5)

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)
 statement

where "prop" is an arbitrary variable that will hold the name of the current property of the object as it is being looped. The value of the property is accessed using object[prop]. During each iteration, "prop" is overwritten with the current property's name, and the body statement(s) of the for/in loop executed.

For built in objects, for/in may not necessarily loop through every property within it, but rather, only ones that are enumerable. With custom objects, all of its properties are enumerable btw.

Here are two examples.

Example 1: This example writes out all the properties of the "navigator" object, plus their values:

for (var myprop in navigator){
 document.write(myprop+": "+navigator[myprop]+"<br>")
}

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}}
for (var attr in userprofile){
 document.write("<b>"+attr+":</b> "+userprofile[attr]+"<br />")
}

Output:

Notice how the method getage() also gets output, as strictly speaking, methods are also properties of an object.

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++){
 if (puppies[i].name=="George")
  alert("I found George!")
 break
}

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++){
 if (i==4)
  continue
 document.write(i+"<br>")
}
//1, 2, 3, 5 is written out.


Reference List

Right column

CopyRight (c) 2018 JavaScript Kit. NO PART may be reproduced without author's permission. Contact Info