## Description
An *iterator protocol* describes a way to produce a sequence of values and potentially a return value when all values have been generated.
An object is an *iterator* when it implements the `next()` method and returns an *IteratorResult*.
```JavaScript
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
```
An *iterator* can also become an [[Iterable Protocol|iterable]] by implementing `[@@iterator]()` and returning `this`.
```JavaScript
// Satisfies both the Iterator Protocol and Iterable
const myIterator = {
next() {
// ...
},
[Symbol.iterator]() {
return this;
},
};
```
## See Also
- [[Iterable Protocol]]
## References
- [Iterator Protocol - MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol)