Creating a Custom Iterator

Collection objects (including Array, Set, and Map) come with built-in iterators that allow us to navigate their values. So, we don’t have to create our own iterators. But sometimes these objects don’t serve our purpose. What if we want to customize the iteration behavior of an object to return values backward or randomly? Or iterate over a plain object or class, both being not iterable by default? In that case, we’ll need to define our own Symbol.iterator.

Set and Map

images/aside-icons/info.png ES2015 introduced two new data structures: Set and Map. A Set is like an array, a collection of values, but it can’t contain duplicates and the items cannot ...

Get Modern Asynchronous JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.