for...of

Iterates over the values of an iterable such as an array, string, Map, or Set.

Since ES2015 (ES6) Spec ↗

Syntax

for (const value of iterable) {
  // ...
}

Throws

  • TypeError — The right-hand operand is not iterable.

Examples

for (const c of "hi") {
  console.log(c);
}
Output
h
i
const nums = [10, 20, 30];
for (const n of nums) {
  console.log(n);
}
Output
10
20
30
const m = new Map([["a", 1], ["b", 2]]);
for (const [k, v] of m) {
  console.log(k, v);
}
Output
a 1
b 2

Notes

- Works on any iterable: arrays, strings, `Map`, `Set`, arguments, NodeLists, generators, etc. - To get the index alongside the value, use `array.entries()`. - Use `for...in` to iterate enumerable property keys instead of values.

Browser & runtime support

EnvironmentSince version
chrome 38
firefox 13
safari 7
edge 12
node 0.12

See also