for/in Loops

Another type of for loop is the for/in loop. The for/in loop executes on any data type that can be iterated. For the most part, you will use for/in loops on arrays and objects. The following example illustrates the syntax and behavior of the for/in loop on a simple array:

var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
for (var idx in days){
  console.log("It's " + days[idx] + "<br>");
}

Notice that the variable idx is adjusted each iteration through the loop, from the beginning array index to the last. The resulting output is:

It's Monday
It's Tuesday
It's Wednesday
It's Thursday
It's Friday

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.118.150