JavaScript Custom Iterables Explained

Very cool JS feature, although something you’ll probably never (or shouldn’t 😅) write in production: custom iterables. If an object implements [Symbol.iterator], it plugs into JavaScript’s iteration protocol, so it works naturally with for...of and spread. 👨💻✨💡 #javascript

  • text

It seems fine for production. I just have to ask myself, when would I want a new kind of iterable object rather than just a generator function? The advantage of a Range object over `function* range(from, to)` is that I can retrieve `range.from` and `range.to` later, I suppose. I can't see it being the clearest way often, but maybe sometimes!

The production equivalent are generators

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories