For…In Statement : Loop Through the properties of an object
- The for…in statement loops through the properties of an object.
- The code in the body of the for…in loop is executed once for each property.
Live Example : For..In Statement
<html> <body> <script type="text/javascript"> var book = { bname : "Java Programming", author : "Pritesh Taral", pages : 275 }; var x; for (x in book) { document.write(book[x] + ""); } </script> </body> </html>
Output :
Java Programming Pritesh Taral 275
Explanation :
- We have declared object book having 3 properties.
- Properties are - bname,author and pages.
- x is declared as index variable which is used while iterating through object properties.
- For..In Loop will print values of all properties in a sequence in which they are declared.