Control Flow

For Each in JavaScript

Used iterate through all elements in an array or object.


Syntax
//iterator variable is declared in the for each loop
for (var iteratorVariable of object) {
    //the individual element is held in the iterator variable
    //to access the value, just use iteratorVariable
}

Notes

Each element is represented by a temporary iterator variable, declared in the for-each structure.


Example
var studentGrades = [50, 89, 75]; 

for (var grade of studentGrades) { 
    console.log("Grade: " + grade); 
} //prints each value of the array studentGrades (each loop is an element of studentGrades assigned to grade variable).

See Also
Documentation

for...in - JavaScript | MDN

Add to Slack

< For Loop   |   While Loop >

© 2018 SyntaxDB. All Rights Reserved.