Symbol.iterator and Iterator.next
iterable[Symbol.iterator]()
iterator.next()
iterator.return?.(value)An iterable returns an iterator from Symbol.iterator. next() yields { value, done }. for…of may call return() on early exit.
| Parameter | Description |
|---|---|
value | Optional completion value passed to return() or as the result of a finished iterator. |
Returns: next and return return an iterator result object with a done boolean and a value.
- This page does not call Iterator helper methods. Array, Map, and Set iteration stay on those pages.
const list = [10, 20];
const it = list[Symbol.iterator]();
console.log(JSON.stringify(it.next()));
console.log(JSON.stringify(it.next()));
const last = it.next();
console.log(last.value, last.done);Expected output
{"value":10,"done":false}
{"value":20,"done":false}
undefined trueRelated: Generators, Symbol, Array, Map and Set, Official iteration protocols