Unit 5. Iterables
In JavaScript, Strings and Arrays have always had a couple of things in common. They both contain an indefinite amount of stuff—characters in the case of strings and any datatype in the case of arrays. They both also have a length property indicating how many items they have. But there was never a common protocol that described how these things worked. Starting in ES2015, there are two new protocols that describe these JavaScript behaviors, known as the iterable and iterator protocols.
Strings and Arrays are now known as iterables. This means they adhere to the new iterable protocol and can be predictably interacted with in common ways, including being used with the new for..of statement and the new spread operator. There are also two new iterables: Maps and Sets. Additionally, you can define your own iterables or even customize the behavior of the built-in ones. And because they all follow the iterable protocol, they will always behave in predictable ways.
We‘ll start the unit by looking at the iterable protocol itself: how it works, how to create your own iterables, and how to use iterables with the for..of statement and spread operator. You’ll then learn about the new built-in iterable types, Sets and Maps. Finally, you’ll wrap up the unit by building a Blackjack game that make uses of Maps and Sets, as well as using for..of and spread.