Search⌘ K

Iterables & Iterators

Explore how to create and use JavaScript iterables and iterators according to the ES2015 iterable protocol. Understand the role of Symbol.iterator, the structure of iterator objects with next methods, and how these enable for-of loops and the spread operator. This lesson helps you manually iterate over custom and built-in iterable objects and grasp the importance of stop conditions in iteration processes.

We'll cover the following...

ES2015 introduced the iterable protocol. This is a way for objects to describe how they should behave when under iteration, or when we are trying to access their elements.

JavaScript Iterables

In JavaScript, an iterable is an object that has the following qualities:

  • Has a property method, the key for which is Symbol.iterator. The method should:

    • Return an iterator. An iterator is an object with a next method. An iterator’s next method should return an object that has the following properties:

      • value, any type
      • done, a Boolean

Let’s build up an ...