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.)

Karel Bílek
  • 36,467
  • 31
  • 94
  • 149
  • 3
    http://stackoverflow.com/questions/6863182/what-is-the-difference-between-iterator-and-iterable-and-how-to-use-them – Iłya Bursov Apr 26 '16 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.) – Karel Bílek Apr 26 '16 at 19:59
  • 3
    @Lashane that question is about Java, I am using ES6, that's a different language – Karel Bílek Apr 26 '16 at 20:00
  • 1
    the proposed duplicate is about another similar language, where the concept is identical (possibly replicated from). – Claies Apr 26 '16 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? – Conduit Apr 26 '16 at 20:02
  • @Claies OK, that is a real answer I was looking for. That it's the same in ES6 as in Java :) Thanks! – Karel Bílek Apr 26 '16 at 20:03

1 Answers1

10

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).

Community
  • 1
  • 1
  • Both this answer and the proposed duplicate helped me, so I marked this question as answering and the proposed duplicate as duplicate – Karel Bílek Apr 26 '16 at 20:14