Course contentsShow
← JavaScript
Lesson 1076 of 2,72325. Iterators and GeneratorsFree lesson

Built-in Iterables: Arrays, Strings, Maps, Sets

Exploring which JavaScript types implement the iterator protocol by default.

Built-in Iterables: Arrays, Strings, Maps, Sets

You'll learn which JavaScript types already come with iterator protocol support out of the box.

The Pre-Packaged Iterables

You don't need to implement Symbol.iterator yourself for many common JavaScript types—they already have it! JavaScript's built-in collections implement the iterator protocol automatically, making them ready to use with for...of loops and other iteration-based features.

Think of it like appliances: Some devices come with batteries included. Similarly, some JavaScript types come with iteration built-in—no assembly required.

Which Types Are Iterable?

Arrays

Arrays have a built-in iterator that yields each element in order:

const arr = [10, 20, 30];
for (const num of arr) {
  console.log(num); // 10, 20, 30
}

Strings

Strings iterate over each character (technically, each Unicode code point):

const text = "Hi!";
for (const char of text) {
  console.log(char); // "H", "i", "!"
}

Maps

Maps iterate over [key, value] pairs:

const map = new Map([['a', 1], ['b', 2]]);
for (const [key, value] of map) {
  console.log(key, value); // "a" 1, then "b" 2
}

Sets

Sets iterate over unique values:

const set = new Set([5, 10, 15]);
for (const val of set) {
  console.log(val); // 5, 10, 15
}

What About Plain Objects?

Interestingly, plain objects ({}) are not iterable by default! They don't implement Symbol.iterator, so you can't use for...of directly on them.

Key Takeaway: Arrays, Strings, Maps, and Sets all implement the iterator protocol automatically, making them work seamlessly with for...of loops and other iteration features—but plain objects do not.