Search code examples
javascriptloopsiteratoriterable

How are custom Iterables useful in JS? What are some common usecases of thing that can't be done easier any other way?


I'm taking about making you own iterable using [Symbol.iterator] - I know how it technically works, but I cannot think of a genuine usecase. What can you do with this?


Solution

  • The most common use case is to create a custom iterator for a custom data structure. For example, if you have a tree data structure, you can create an iterator that traverses the tree in a specific order (e.g. depth-first).