Can anyone explain with example how does a 'for/in' loop work?


#1

Can anyone explain with example how does a 'for/in' loop work?


#2

A for/in loop is to sort through items in a variable. Say you had a list called grades and you wanted all of the grades < 90% to be in another list called a_grades. You would use a for/in loop as shown below.

//Grades List
var grades = [95, 87, 60, 91, 97, 100, 100, 76, 84];
//Loop
for(var i in grades) {
    if (i > 90) {
        var a_grades = [];
        a_grades.push[i];
    }
}
console.log(a_grades);

This would output:

[95, 91, 97, 100, 100]