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. This is the resulting output:
It's Monday
It's Tuesday
It's Wednesday
It's Thursday
It's Friday