for-of loop

Champion: Dave Herman
Details: ES6 draft section 12.6.4

The new for-of loop is for looping over iterable objects.


Conveniently iterate over data in Arrays, Sets, and Maps:

    for (let word of ["one", "two", "three"])

    let s = new Set([3, 4, 5]);
    for (let value of s)

    let m = new Map([['red', 'rojo'], ['blue', 'azul']]);
    for (let [name, value] of m)
        alert(name + " = " + value);

for-of is also available in array comprehensions.

See iterators for examples of how to create custom iterable objects.

How for-of is different from for-in

The for-of and for-in loops look similar, but they do two very different things.

  • for-in is for inspecting object properties. It works on any object, and it always loops over the names of the object's enumerable properties.

  • for-of is for looping over data. It only works on iterable objects; that is, objects with a suitable [iterator] method.

    var colors = new Set(['rojo', 'amarillo', 'azul']);
    colors.language = 'es';     // add an expando property

    for (let name in colors)
        alert(name);            // "language" (the property name)

    for (let word of colors)
        alert(word);            // "rojo", "amarillo", "azul" (the data)