please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 5
Statements
Next
 

5.6 for...in

The for keyword is used in two ways in JavaScript. We've just seen how it is used in the for loop. It is also used in the for/in statement. This statement is a somewhat different kind of loop with the following syntax:

for (variable in object)
    statement

The variable should be the name of a variable, or should be an element of an array or a property of an object; it should be something suitable as the left-hand side of an assignment expression. object is the name of an object, or an expression that evaluates to an object. As usual, the statement is a primitive statement or statement block that forms the body of the loop.

You can loop through the elements of an array by simply incrementing an index variable each time through a while or for loop. The for/in statement provides a way to loop through the properties of an object. The body of the for/in loop is executed once for each property of object. Before the body of the loop is executed, the name of one of the object's properties is assigned to variable, as a string. Within the body of the loop, you can use this variable to look up the value of the object's property with the [] operator. For example, the following for/in loop prints out the name and value of each property of an object:

for (prop in my_object) {
    document.write("name: " + prop + "; value: " + my_object[prop], "<br>");
}

The for/in loop does not specify in what order the properties of an object will be assigned to the variable. There is no way to tell in advance, and the behavior may differ between implementations or versions of JavaScript.

The for/in loop does not actually loop through all possible properties of all objects. The rules below specify exactly which properties the statement does list and which it does not in Navigator 3.0. Internet Explorer may use somewhat different rules:


Previous Home Next
for Book Index break

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

See also:

[Chapter 5] 5.8 continue +


file: /Techref/language/JAVA/SCRIPT/definitive/ch05_06.htm, 8KB, , updated: 2019/10/14 15:00, local time: 2024/12/21 20:24,
TOP NEW HELP FIND: 
3.138.67.56:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://massmind.org/techref/language/JAVA/SCRIPT/definitive/ch05_06.htm"> [Chapter 5] 5.6 for...in</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to massmind.org!

 

Welcome to massmind.org!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .