Iterator.prototype[@@iterator]()
The [@@iterator]()
method of Iterator
instances implements the iterable protocol and allows built-in iterators to be consumed by most syntaxes expecting iterables, such as the spread syntax and for...of
loops. It returns the value of this
, which is the iterator object itself.
Try it
Syntax
js
iterator[Symbol.iterator]()
Return value
The value of this
, which is the iterator object itself.
Examples
Iteration using for...of loop
Note that you seldom need to call this method directly. The existence of the @@iterator
method makes all built-in iterators iterable, and iterating syntaxes like the for...of
loop automatically calls this method to obtain the iterator to loop over.
js
const arrIterator = [1, 2, 3].values();
for (const value of arrIterator) {
console.log(value);
}
// Logs: 1, 2, 3
Specifications
Specification |
---|
ECMAScript Language Specification # sec-%iteratorprototype%-@@iterator |
Browser compatibility
BCD tables only load in the browser