The following are all built-in JavaScript iterators:
- The Array Iterator returned by
Array.prototype[Symbol.iterator]()
,TypedArray.prototype[Symbol.iterator]()
, andarguments[Symbol.iterator]()
. - The String Iterator returned by
String.prototype[Symbol.iterator]()
. - The Map Iterator returned by
Map.prototype[Symbol.iterator]()
. - The Set Iterator returned by
Set.prototype[Symbol.iterator]()
. - The RegExp String Iterator returned by
String.prototype.matchAll()
. - The generator functions.
- The Segments Iterator returned by the
Intl.Segmenter.prototype.segment()
. - The Iterator Helper returned by iterator helper methods such as
Iterator.prototype.map()
.
Web APIs may return iterators too. Some reuse core JavaScript iterators while others define their own iterators. For example:
NodeList
return an Array Iterator from their respective methodskeys()
,values()
,entries()
, and[Symbol.iterator]()
.Headers
return their own iterator type like Headers Iterator from their respective methodskeys()
,values()
,entries()
, and[Symbol.iterator]()
.FontFaceSet
return their own iterator type like FontFaceSet Iterator from their respective methodskeys()
,values()
,entries()
, and[Symbol.iterator]()
.
Note: iterable protocol.
Each of these iterators have a distinct prototype object, which defines the next()
method used by the particular iterator. For example, all string iterator objects inherit from a hidden object StringIteratorPrototype
, which has a next()
method that iterates this string by code points. StringIteratorPrototype
also has a Object.prototype.toString()
. Similarly, other iterator prototypes also have their own [Symbol.toStringTag]
values, which are the same as the names given above.
All of these prototype objects inherit from Iterator.prototype
, which provides a iterable.