4. for..in Loop


          The for..in loop is a more concise way of iterating over an object's properties. The for...in loop will run for all of the object's elements, and its syntax will be as follows:

Syntax:


<script>
  // Initialize object.
Rank = {"Rank1": "Legendary", "Rank2": "Master", "Rank3": "Pro", "Rank4": "Elite", "Rank5": "Veteran", "Rank6": "Rookie"};

// Iterate over the properties.
var cod = "";
for (var codRank in Rank) {
            cod += codRank + ": " + Rank[codRank];
           cod += "<br />";
  }
document.write(cod);
 </script>


Output:

          Example 2: shows how to initialize the array with expanding properties to the array and iterate over the properties and elements using the for..in loop.

<script>
// Initialize the array.
var arr = new Array("Legendary", "Master", "Pro");

// Add a few expand properties to the array.
arr["Rank3"] = "Elite";
arr["Rank4"] = "Veteran";

// Iterate over the properties and elements.
var cod = "";
for (var codRank in arr) {
cod += codRank + ": " + arr[codRank];
cod += "<br />";
}
document.write (cod);
</script>


Output: