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. This is the resulting output:

It's MondayIt's TuesdayIt's WednesdayIt's ThursdayIt's Friday

Get Learning AngularJS now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.