Search code examples
javascriptpointersiteratorgenerator

What is iterator/generator/iterable? (Javascript)


I really have a hard time to understand the core ideas for 'iterator' 'iterable' 'generator'

The main reason that I'm confused is all the articles explaining the concepts seem like feeding one another.

To explain generator, the article says 'iterable' to explain 'iterator', articles uses 'generator' It feels like infinite loop of these three jargons. Can anyone let me know what these three jargons mean?

Thank you.


Solution

  • Probably easy enough to explain by analogy. Lets say you have a bucket of fruits and you are picking fruits out of this bucket, one by one, and passing them to someone, also one by one. The bucket, or the container you are getting your items from - iterable. Your hand you use to pick fruits out one by one - iterator. The entire you, entire mechanism used to pick stuff one by one, and pass that stuff to someone, also one by one - generator. Hope that makes some sense.