Javascript for loop array
Javascrip for ..in statements
The javascript for ..in and continue used to loop through the elements of an array. It is also used to loop through the elements of the object. We will learn it with arrays now. Later on we will have opportunity to use it with objects when we learn that
for .. in Syntax
for (variable in object){
code to be executed
}
The code in the body of the for ... in loop is executed once for each property. The variable argument can be a named variable, an array element, or a property of the object.
Example
<html> <body> <script type="text/javascript"> <!-- /* ******************************************************** Example for ..in ******************************************************** */ var x; var uscities = new Array(); uscities[0] = "Boston"; uscities[1] = "Chicago"; uscities[2] = "Austin"; for (x in uscities) { document.write(uscities[x] + "<br />"); } //--> </script> </body> </html> |
The example will loop through all values of the array uscities. It will therefore produce the result as follows.
Page Next Page
Boston Chicago Austin |