Iteration protocols
Iteration protocols aren't new built-ins or syntax, butprotocols. These protocols can be implemented by any object by following some conventions.
There are two protocols: Theiterable protocol and theiterator protocol.
The iterable protocol
The iterable protocol allows JavaScript objects to define or customize their iteration behavior, such as what values are looped over in afor...of
construct. Some built-in types arebuilt-in iterables with a default iteration behavior, such asArray
orMap
, while other types (such asObject
) are not.
In order to beiterable, an object must implement the[Symbol.iterator]()
method, meaning that the object (or one of the objects up itsprototype chain) must have a property with a[Symbol.iterator]
key which is available via constantSymbol.iterator
:
[Symbol.iterator]()
A zero-argument function that returns an object, conforming to theiterator protocol.
Whenever an object needs to be iterated (such as at the beginning of afor...of
loop), its[Symbol.iterator]()
method is called with no arguments, and the returnediterator is used to obtain the values to be iterated.
Note that when this zero-argument function is called, it is invoked as a method on the iterable object. Therefore inside of the function, thethis
keyword can be used to access the properties of the iterable object, to decide what to provide during the iteration.
This function can be an ordinary function, or it can be a generator function, so that when invoked, an iterator object is returned. Inside of this generator function, each entry can be provided by usingyield
.
The iterator protocol
The iterator protocol defines a standard way to produce a sequence of values (either finite or infinite), and potentially a return value when all values have been generated.
An object is an iterator when it implements anext()
method with the following semantics:
next()
A function that accepts zero or one argument and returns an object conforming to the
IteratorResult
interface (see below). If a non-object value gets returned (such asfalse
orundefined
) when a built-in language feature (such asfor...of
) is using the iterator, aTypeError
("iterator.next() returned a non-object value"
) will be thrown.
All iterator protocol methods (next()
,return()
, andthrow()
) are expected to return an object implementing theIteratorResult
interface. It must have the following properties:
done
OptionalA boolean that's
false
if the iterator was able to produce the next value in the sequence. (This is equivalent to not specifying thedone
property altogether.)Has the value
true
if the iterator has completed its sequence. In this case,value
optionally specifies the return value of the iterator.value
OptionalAny JavaScript value returned by the iterator. Can be omitted when
done
istrue
.
In practice, neither property is strictly required; if an object without either property is returned, it's effectively equivalent to{ done: false, value: undefined }
.
If an iterator returns a result withdone: true
, any subsequent calls tonext()
are expected to returndone: true
as well, although this is not enforced on the language level.
Thenext
method can receive a value which will be made available to the method body. No built-in language feature will pass any value. The value passed to thenext
method ofgenerators will become the value of the correspondingyield
expression.
Optionally, the iterator can also implement thereturn(value)
andthrow(exception)
methods, which, when called, tells the iterator that the caller is done with iterating it and can perform any necessary cleanup (such as closing database connection).
return(value)
OptionalA function that accepts zero or one argument and returns an object conforming to the
IteratorResult
interface, typically withvalue
equal to thevalue
passed in anddone
equal totrue
. Calling this method tells the iterator that the caller does not intend to make any morenext()
calls and can perform any cleanup actions. When built-in language features callreturn()
for cleanup,value
is alwaysundefined
.throw(exception)
OptionalA function that accepts zero or one argument and returns an object conforming to the
IteratorResult
interface, typically withdone
equal totrue
. Calling this method tells the iterator that the caller detects an error condition, andexception
is typically anError
instance. No built-in language feature callsthrow()
for cleanup purposes — it's a special feature of generators for the symmetry ofreturn
/throw
.
Note:It is not possible to know reflectively (i.e., without actually callingnext()
and validating the returned result) whether a particular object implements the iterator protocol.
It is very easy to make an iterator also iterable: just implement an[Symbol.iterator]()
method that returnsthis
.
// Satisfies both the Iterator Protocol and Iterableconst myIterator = { next() { // … }, [Symbol.iterator]() { return this; },};
Such object is called aniterable iterator. Doing so allows an iterator to be consumed by the various syntaxes expecting iterables — therefore, it is seldom useful to implement the Iterator Protocol without also implementing Iterable. (In fact, almost all syntaxes and APIs expectiterables, notiterators.) Thegenerator object is an example:
const aGeneratorObject = (function* () { yield 1; yield 2; yield 3;})();console.log(typeof aGeneratorObject.next);// "function" — it has a next method (which returns the right result), so it's an iteratorconsole.log(typeof aGeneratorObject[Symbol.iterator]);// "function" — it has an [Symbol.iterator] method (which returns the right iterator), so it's an iterableconsole.log(aGeneratorObject[Symbol.iterator]() === aGeneratorObject);// true — its [Symbol.iterator] method returns itself (an iterator), so it's an iterable iterator
All built-in iterators inherit fromIterator.prototype
, which implements the[Symbol.iterator]()
method as returningthis
, so that built-in iterators are also iterable.
However, when possible, it's better foriterable[Symbol.iterator]()
to return different iterators that always start from the beginning, likeSet.prototype[Symbol.iterator]()
does.
The async iterator and async iterable protocols
There are another pair of protocols used for async iteration, namedasync iterator andasync iterable protocols. They have very similar interfaces compared to the iterable and iterator protocols, except that each return value from the calls to the iterator methods is wrapped in a promise.
An object implements the async iterable protocol when it implements the following methods:
[Symbol.asyncIterator]()
A zero-argument function that returns an object, conforming to the async iterator protocol.
An object implements the async iterator protocol when it implements the following methods:
next()
A function that accepts zero or one argument and returns a promise. The promise fulfills to an object conforming to the
IteratorResult
interface, and the properties have the same semantics as those of the sync iterator's.return(value)
OptionalA function that accepts zero or one argument and returns a promise. The promise fulfills to an object conforming to the
IteratorResult
interface, and the properties have the same semantics as those of the sync iterator's.throw(exception)
OptionalA function that accepts zero or one argument and returns a promise. The promise fulfills to an object conforming to the
IteratorResult
interface, and the properties have the same semantics as those of the sync iterator's.
Interactions between the language and iteration protocols
The language specifies APIs that either produce or consume iterables and iterators.
Built-in iterables
String
,Array
,TypedArray
,Map
,Set
, andSegments
(returned byIntl.Segmenter.prototype.segment()
) are all built-in iterables, because each of theirprototype
objects implements an[Symbol.iterator]()
method. In addition, thearguments
object and some DOM collection types such asNodeList
are also iterables.There is no object in the core JavaScript language that is async iterable. Some web APIs, such asReadableStream
, have theSymbol.asyncIterator
method set by default.
Generator functions returngenerator objects, which are iterable iterators.Async generator functions returnasync generator objects, which are async iterable iterators.
The iterators returned from built-in iterables actually all inherit from a common classIterator
, which implements the aforementioned[Symbol.iterator]() { return this; }
method, making them all iterable iterators. TheIterator
class also provides additionalhelper methods in addition to thenext()
method required by the iterator protocol. You can inspect an iterator's prototype chain by logging it in a graphical console.
console.log([][Symbol.iterator]());Array Iterator {} [[Prototype]]: Array Iterator ==> This is the prototype shared by all array iterators next: ƒ next() Symbol(Symbol.toStringTag): "Array Iterator" [[Prototype]]: Object ==> This is the prototype shared by all built-in iterators Symbol(Symbol.iterator): ƒ [Symbol.iterator]() [[Prototype]]: Object ==> This is Object.prototype
Built-in APIs accepting iterables
There are many APIs that accept iterables. Some examples include:
Map()
WeakMap()
Set()
WeakSet()
Promise.all()
Promise.allSettled()
Promise.race()
Promise.any()
Array.from()
Object.groupBy()
Map.groupBy()
const myObj = {};new WeakSet( (function* () { yield {}; yield myObj; yield {}; })(),).has(myObj); // true
Syntaxes expecting iterables
Some statements and expressions expect iterables, for example thefor...of
loops,array and parameter spreading,yield*
, andarray destructuring:
for (const value of ["a", "b", "c"]) { console.log(value);}// "a"// "b"// "c"console.log([..."abc"]); // ["a", "b", "c"]function* gen() { yield* ["a", "b", "c"];}console.log(gen().next()); // { value: "a", done: false }[a, b, c] = new Set(["a", "b", "c"]);console.log(a); // "a"
When built-in syntaxes are iterating an iterator, and the last result'sdone
isfalse
(i.e., the iterator is able to produce more values) but no more values are needed, thereturn
method will get called if present. This can happen, for example, if abreak
orreturn
is encountered in afor...of
loop, or if all identifiers are already bound in an array destructuring.
const obj = { [Symbol.iterator]() { let i = 0; return { next() { i++; console.log("Returning", i); if (i === 3) return { done: true, value: i }; return { done: false, value: i }; }, return() { console.log("Closing"); return { done: true }; }, }; },};const [a] = obj;// Returning 1// Closingconst [b, c, d] = obj;// Returning 1// Returning 2// Returning 3// Already reached the end (the last call returned `done: true`),// so `return` is not calledconsole.log([b, c, d]); // [1, 2, undefined]; the value associated with `done: true` is not reachablefor (const b of obj) { break;}// Returning 1// Closing
Thefor await...of
loop andyield*
inasync generator functions (but notsync generator functions) are the only ways to interact with async iterables. Usingfor...of
, array spreading, etc. on an async iterable that's not also a sync iterable (i.e., it has[Symbol.asyncIterator]()
but no[Symbol.iterator]()
) will throw a TypeError: x is not iterable.
Error handling
Because iteration involves transferring control back and forth between the iterator and the consumer, error handling happens in both ways: how the consumer handles errors thrown by the iterator, and how the iterator handles errors thrown by the consumer. When you are using one of the built-in ways of iteration, the language may also throw errors because the iterable breaks certaininvariants. We will describe how built-in syntaxes generate and handle errors, which can be used as a guideline for your own code if you are manually stepping the iterator.
Non-well-formed iterables
Errors may happen when acquiring the iterator from the iterable. The language invariant enforced here is that the iterable must produce a valid iterator:
- It has a callable
[Symbol.iterator]()
method. - The
[Symbol.iterator]()
method returns an object. - The object returned by
[Symbol.iterator]()
has a callablenext()
method.
When using built-in syntax to initiate iteration on a non-well-formed iterable, a TypeError is thrown.
const nonWellFormedIterable = { [Symbol.iterator]: 1 };[...nonWellFormedIterable]; // TypeError: nonWellFormedIterable is not iterablenonWellFormedIterable[Symbol.iterator] = () => 1;[...nonWellFormedIterable]; // TypeError: [Symbol.iterator]() returned a non-object valuenonWellFormedIterable[Symbol.iterator] = () => ({});[...nonWellFormedIterable]; // TypeError: nonWellFormedIterable[Symbol.iterator]().next is not a function
For async iterables, if its[Symbol.asyncIterator]()
property has valueundefined
ornull
, JavaScript falls back to using the[Symbol.iterator]
property instead (and wraps the resulting iterator into an async iterator byforwarding the methods). Otherwise, the[Symbol.asyncIterator]
property must conform to the above invariants too.
This type of errors can be prevented by first validating the iterable before attempting to iterate it. However, it's fairly rare because usually you know the type of the object you are iterating over. If you are receiving this iterable from some other code, you should just let the error propagate to the caller so they know an invalid input was provided.
Errors during iteration
Most errors happen when stepping the iterator (callingnext()
). The language invariant enforced here is that thenext()
method must return an object (for async iterators, an object after awaiting). Otherwise, a TypeError is thrown.
If the invariant is broken or thenext()
method throws an error (for async iterators, it may also return a rejected promise), the error is propagated to the caller. For built-in syntaxes, the iteration in progress is aborted without retrying or cleanup (with the assumption that if thenext()
method threw the error, then it has cleaned up already). If you are manually callingnext()
, you may catch the error and retry callingnext()
, but in general you should assume the iterator is already closed.
If the caller decides to exit iteration for any reason other than the errors in the previous paragraph, such as when it enters an error state in its own code (for example, while handling an invalid value produced by the iterator), it should call thereturn()
method on the iterator, if one exists. This allows the iterator to perform any cleanup. Thereturn()
method is only called for premature exits—ifnext()
returnsdone: true
, thereturn()
method is not called, with the assumption that the iterator has already cleaned up.
Thereturn()
method might be invalid too! The language also enforces that thereturn()
method must return an object and throws a TypeError otherwise. If thereturn()
method throws an error, the error is propagated to the caller. However, if thereturn()
method is called because the caller encountered an error in its own code, then this error overrides the error thrown by thereturn()
method.
Usually, the caller implements error handling like this:
try { for (const value of iterable) { // … }} catch (e) { // Handle the error}
Thecatch
will be able to catch errors thrown wheniterable
is not a valid iterable, whennext()
throws an error, whenreturn()
throws an error (if thefor
loop exits early), and when thefor
loop body throws an error.
Most iterators are implemented with generator functions, so we will demonstrate how generator functions typically handle errors:
function* gen() { try { yield doSomething(); yield doSomethingElse(); } finally { cleanup(); }}
The lack ofcatch
here causes errors thrown bydoSomething()
ordoSomethingElse()
to propagate to the caller ofgen
. If these errors are caught within the generator function (which is equally advisable), the generator function can decide to continue yielding values or to exit early. However, thefinally
block is necessary for generators that keep open resources. Thefinally
block is guaranteed to run, either when the lastnext()
is called or whenreturn()
is called.
Forwarding errors
Some built-in syntaxes wrap an iterator into another iterator. They include the iterator produced byIterator.from()
,iterator helper methods (map()
,filter()
,take()
,drop()
, andflatMap()
),yield*
, and a hidden wrapper when you use async iteration (for await...of
,Array.fromAsync
) on sync iterators. The wrapped iterator is then responsible for forwarding errors between the inner iterator and the caller.
- All wrapper iterators directly forward the
next()
method of the inner iterator, including its return value and thrown errors. - Wrapper iterators generally directly forward the
return()
method of the inner iterator. If thereturn()
method doesn't exist on the inner iterator, it returns{ done: true, value: undefined }
instead. In the case of iterator helpers: if the iterator helper'snext()
method has not been called, after trying to callreturn()
on the inner iterator, the current iterator always returns{ done: true, value: undefined }
. This is consistent with generator functions where execution hasn't entered theyield*
expression yet. yield*
is the only built-in syntax that forwards thethrow()
method of the inner iterator. For information on howyield*
forwards thereturn()
andthrow()
methods, see its own reference.
Examples
User-defined iterables
You can make your own iterables like this:
const myIterable = { *[Symbol.iterator]() { yield 1; yield 2; yield 3; },};console.log([...myIterable]); // [1, 2, 3]
Basic iterator
Iterators are stateful by nature. If you don't define it as agenerator function (as the example above shows), you would likely want to encapsulate the state in a closure.
function makeIterator(array) { let nextIndex = 0; return { next() { return nextIndex < array.length ? { value: array[nextIndex++], done: false, } : { done: true, }; }, };}const it = makeIterator(["yo", "ya"]);console.log(it.next().value); // 'yo'console.log(it.next().value); // 'ya'console.log(it.next().done); // true
Infinite iterator
function idMaker() { let index = 0; return { next() { return { value: index++, done: false, }; }, };}const it = idMaker();console.log(it.next().value); // 0console.log(it.next().value); // 1console.log(it.next().value); // 2// …
Defining an iterable with a generator
function* makeGenerator(array) { let nextIndex = 0; while (nextIndex < array.length) { yield array[nextIndex++]; }}const gen = makeGenerator(["yo", "ya"]);console.log(gen.next().value); // 'yo'console.log(gen.next().value); // 'ya'console.log(gen.next().done); // truefunction* idMaker() { let index = 0; while (true) { yield index++; }}const it = idMaker();console.log(it.next().value); // 0console.log(it.next().value); // 1console.log(it.next().value); // 2// …
Defining an iterable with a class
State encapsulation can be done withprivate fields as well.
class SimpleClass { #data; constructor(data) { this.#data = data; } [Symbol.iterator]() { // Use a new index for each iterator. This makes multiple // iterations over the iterable safe for non-trivial cases, // such as use of break or nested looping over the same iterable. let index = 0; return { // Note: using an arrow function allows `this` to point to the // one of `[Symbol.iterator]()` instead of `next()` next: () => { if (index >= this.#data.length) { return { done: true }; } return { value: this.#data[index++], done: false }; }, }; }}const simple = new SimpleClass([1, 2, 3, 4, 5]);for (const val of simple) { console.log(val); // 1 2 3 4 5}
Overriding built-in iterables
For example, aString
is a built-in iterable object:
const someString = "hi";console.log(typeof someString[Symbol.iterator]); // "function"
String
'sdefault iterator returns the string's code points one by one:
const iterator = someString[Symbol.iterator]();console.log(`${iterator}`); // "[object String Iterator]"console.log(iterator.next()); // { value: "h", done: false }console.log(iterator.next()); // { value: "i", done: false }console.log(iterator.next()); // { value: undefined, done: true }
You can redefine the iteration behavior by supplying our own[Symbol.iterator]()
:
// need to construct a String object explicitly to avoid auto-boxingconst someString = new String("hi");someString[Symbol.iterator] = function () { return { // this is the iterator object, returning a single element (the string "bye") next() { return this._first ? { value: "bye", done: (this._first = false) } : { done: true }; }, _first: true, };};
Notice how redefining[Symbol.iterator]()
affects the behavior of built-in constructs that use the iteration protocol:
console.log([...someString]); // ["bye"]console.log(`${someString}`); // "hi"
Concurrent modifications when iterating
Almost all iterables have the same underlying semantic: they don't copy the data at the time when iteration starts. Rather, they keep a pointer and move it around. Therefore, if you add, delete, or modify elements in the collection while iterating over the collection, you may inadvertently change whether otherunchanged elements in the collection are visited. This is very similar to howiterative array methods work.
Consider the following case using aURLSearchParams
:
const searchParams = new URLSearchParams( "deleteme1=value1&key2=value2&key3=value3",);// Delete unwanted keysfor (const [key, value] of searchParams) { console.log(key); if (key.startsWith("deleteme")) { searchParams.delete(key); }}// Output:// deleteme1// key3
Note how it never logskey2
. This is because aURLSearchParams
is underlyingly a list of key-value pairs. Whendeleteme1
is visited and deleted, all other entries are shifted to the left by one, sokey2
occupies the position thatdeleteme1
used to be in, and when the pointer moves to the next key, it lands onkey3
.
Certain iterable implementations avoid this problem by setting "tombstone" values to avoid shifting the remaining values. Consider the similar code using aMap
:
const myMap = new Map([ ["deleteme1", "value1"], ["key2", "value2"], ["key3", "value3"],]);for (const [key, value] of myMap) { console.log(key); if (key.startsWith("deleteme")) { myMap.delete(key); }}// Output:// deleteme1// key2// key3
Note how it logs all keys. This is becauseMap
doesn't shift the remaining keys when one is deleted. If you want to implement something similar, here's how it may look:
const tombstone = Symbol("tombstone");class MyIterable { #data; constructor(data) { this.#data = data; } delete(deletedKey) { for (let i = 0; i < this.#data.length; i++) { if (this.#data[i][0] === deletedKey) { this.#data[i] = tombstone; return true; } } return false; } *[Symbol.iterator]() { for (const data of this.#data) { if (data !== tombstone) { yield data; } } }}const myIterable = new MyIterable([ ["deleteme1", "value1"], ["key2", "value2"], ["key3", "value3"],]);for (const [key, value] of myIterable) { console.log(key); if (key.startsWith("deleteme")) { myIterable.delete(key); }}
Warning:Concurrent modifications, in general, are very bug-prone and confusing. Unless you know precisely how the iterable is implemented, it's best to avoid modifying the collection while iterating over it.
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-iteration |