JavaScript

JavaScript: For…in Loop

JavaScript: For…in Loop

There are many properties in an object. When you have to loop through a particular object’s properties, then for…in the loop is used. 

Following is the syntax used for for…in loop  in JavaScript code:

for (variablename in object) {
   The Statement(s) or the block that has to be executed
}

When the iteration of the properties' properties takes place, each property is provided with a unique variable name, and the exhaustion of the respective process (or a loop) takes place when each property of the object is provided with a unique variable name. 

Following is an example of a JavaScript code using above mentioned for…in the loop:

<html>
   <body>      
      <script type = "text/javascript">
         <!--
            var aProperty;
            document.write("RegExp <br /> ");        
            for (aProperty in RegExp) {
               document.write(aProperty);
               document.write("<br />");
            }
            document.write ("Loop Ended");
         //-->
      </script>      
   </body>
</html>

The output for the program is:

RegExp 

input

multiline

lastMatch

lastParen

leftContext

rightContext

$1

$2

$3

$4

$5

$6

$7

$8

$9

Loop Ended