← Back to Roadmap

Iterables

Medium

In plain terms

Iterables implement Symbol.iterator and can be used with for...of, spread, destructuring. Arrays, strings, Map, Set are iterable. Plain objects are not by default.

What you need to know

  • Symbol.iterator protocol
  • for...of, spread
  • Arrays, strings, Map, Set

Try it yourself

Copy the code below and run it in your browser console or a code editor:

const arr = [1, 2, 3];
[...arr];  // spread works

const str = 'hi';
[...str];  // ['h', 'i']

const map = new Map([['a',1], ['b',2]]);
for (const [k, v] of map) { }

// Object not iterable
// for (const x of {}) {}  // Error
Object.entries(obj);  // convert to iterable

Learn more

Dive deeper with these trusted resources: