Two main reasons:
Language features such as for/of
, spread operator, destructing assignment, etc... can be built that work on any object that supports a standard iterator interface.
Programmers using a collection can either use an iterator directly or use it indirectly through the language features that depend upon it and they can interface generically with a collection in many ways without having to know what type of collection it is.
As an example of a language feature in ES6, that uses the iterator standard in the language is that you can have a language construct such as:
for (x of someObj) { ... }
And the for/of
logic only uses the iterator interface on someObj
. It doesn't have to know anything specific about all the types of objects it can iterate. So, because of that, you can use for/of
with ANY kind of object that supports a standard iterator. For example, you can use it with an array, a map, a set, or any custom object you create that supports the iterator interface.
The iterator implementation on a given object defines what elements will be iterable and what order they will be presented in.
Frankly, I find it much more common to use language features that rely on the iterator such as for/of
than to actually use an iterator directly so that I can use a collection in a standard way without having to know what type of collection it is.
In my own coding, I've found it very convenient to be able to iterate an object without knowing what kind of object it is. That allows me to design an interface that can be passed either a Set
or an Array
or sometimes even a Map
and as long as all I want to do is to iterate the items in the collection, I don't have to care which it is or code any differently. If the caller happens to have a Set
handy, they don't have to convert it to an Array
just to call the API - they can just pass the Set
. And, the code in the API doesn't have to examine the type it was passed to adapt its behavior.
Iterators are also lazy evaluated. So, there's no need to build a temporary array of the entire collection before you start iterating. This can make iterating a lot more efficient, particular if the collection is large and/or the iteration doesn't go all the way through the collection.
Iterators can be infinite. For example, you could have a Fibonacci iterator that represents an infinite sequence.
Other ES6 features beyond for/of
that make use of iterators are:
spread syntax
yield*
destructuring assignment
Array.from()
These features can be used with any object that supports a standard iterator.
See these articles for more detail:
Iterables and iterators in ECMAScript 6
ES6 Iterators and Generators in Practice