JS: Iterable Object

By Xah Lee. Date: . Last updated: .

New in JS2015.

What is Iteratable Object

An iterable object holds special values to be iterated over.

An iterable object can have potentially infinite number of items, computed at the time when needed. As opposed to a array, where every item already exists and occupy memory.

Example of Iterable's Values

Iterated values can be anything the iteratable object is designed for, such a list of prime numbers.

How to Iterate Over Iterable's Values

Standard Iterable Objects

const is_iterable = ((x) => (Reflect.has(x, Symbol.iterator)));

console.log(
  is_iterable(String.prototype),
  is_iterable(Array.prototype),
  is_iterable(Set.prototype),
  is_iterable(Map.prototype),
);

The Iterable Interface

Technically, any object that conforms to the Iterable Interface is called Iterable.

Define Your Own Iterable Object

You can create your own iterable object using Generator Function .

JavaScript, Iterable