JavaScript Iterator Reference
The Iterator Object
An Iterator is an object that provides a standard way to access elements sequentially.
An Iterator must adheres to the Iterator Protocol and must have a next() method.
The Iterator.from() Method
The Iterator.from()
creates an iterator object from an existing iterable or iterator object.
Example
// Create an iterator
const myIterator = Iterator.from("123456789");
// Iterate over all elements
let text = "";
for (const x of myIterator) {
text += x;
}
Try it Yourself »
Note
Technically, iterables must implement the Symbol.iterator
method.
In JavaScript the following are iterables:
- Strings
- Arrays
- Typed Arrays
- Sets
- Maps
Because their prototype objects have a
Symbol.iterator
method:
Helper Functions
JavaScript 2025 (ECMAScript 2025) officially approved a set of new Iterator Helper methods that significantly enhance the functionality of iterators in JavaScript:
Function | Description |
---|---|
drop() | Returns an iterator that skips a specified number of elements before yielding the rest |
every() | Returns true if all elements satisfy a test function |
filter() | Returns an iterator containing elements that satisfy a filter function |
find() | Returns the first element that satisfies a test function |
forEach() | Executes a function once for each element in the iterator. |
map() | Returns an iterator with all elements transformed by a map function |
flatMap() | Returns an iterator by mapping each element and then flattening the results |
reduce() | Applies a reducer function against each element to reduce it to a single value |
some() | Returns true if at least one element satisfy a test function |
take() | Returns an iterator that yields a specified number of elements |