13

What is the difference between Iterator and Iterable? Is one sub-type of the other or not? What are the actual differences in some real applications?

I am trying to read some tutorials and specifications, but they are all very complicated.

(I am using ES6 and Babel, if that helps.)

6
  • 3
    stackoverflow.com/questions/6863182/… Commented Apr 26, 2016 at 19:59
  • 1
    To those voting for closing: How is this too broad? I am asking for two related, concrete concepts in a specific language. (I am not complaining, just asking for more info.) Commented Apr 26, 2016 at 19:59
  • 3
    @Lashane that question is about Java, I am using ES6, that's a different language Commented Apr 26, 2016 at 20:00
  • 1
    the proposed duplicate is about another similar language, where the concept is identical (possibly replicated from). Commented Apr 26, 2016 at 20:01
  • 2
    From a language-independent standpoint: an iterator is an object you can use to iterate over the contents of a container, and an iterable is an object that can provide an iterator for its contents. Unsure if that will help, but perhaps it will? Commented Apr 26, 2016 at 20:02

1 Answer 1

11

From Exploring ES6 by Dr. Axel Rauschmayer:

An iterable is a data structure that wants to make its elements accessible to the public. It does so by implementing a method whose key is Symbol.iterator. That method is a factory for iterators.

An iterator is a pointer for traversing the elements of a data structure (think cursors in databases).

Sign up to request clarification or add additional context in comments.

1 Comment

Both this answer and the proposed duplicate helped me, so I marked this question as answering and the proposed duplicate as duplicate

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.