JS: Iterable Object

By Xah Lee. Date: . Last updated: .

(new in JS: ECMAScript 2015)

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.

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

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

console.log(Reflect.has(String.prototype, Symbol.iterator));
console.log(Reflect.has(Array.prototype, Symbol.iterator));
console.log(Reflect.has(Set.prototype, Symbol.iterator));
console.log(Reflect.has(Map.prototype, Symbol.iterator));

// true

Define Your Own Iterable Object

You can create your own iterable object using Generator Function .

JavaScript. Iterable, Iterator