jskatas.org Continuously Learn JavaScript. Your Way.

Iterator: protocol

Donate to NGO Julenka. Support Ukranians in need. Julenka is an NGO which my brother founded in 2011 to support Ukranian families and kids in need.

the iteratorFunction needs to comply to the iterator protocol

  • must return an object
  • the object must have a function assigned to a key next
  • calling next() must return an object with {done: true}

the iterable

  • must be an object
  • must have the iterator function assigned to the key Symbol.iterator

using the iterable

  • it contains no values
  • has no .length property

can be converted to an array

  • using Array.from()
  • where .length is still 0