Iterator.from()

Baseline 2025
Newly available

Since March 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

The Iterator.from() static method creates a new Iterator object from an iterator or iterable object.

Syntax

js
Iterator.from(object)

Parameters

object

An object that implements the iterable protocol or the iterator protocol.

Return value

If object is an iterable, its [Symbol.iterator]() method is called to obtain the iterator. Otherwise, object is assumed to be an iterator. If the iterator is already instanceof Iterator (which means it has Iterator.prototype in its prototype chain), it is returned directly. Otherwise, a new Iterator object is created that wraps the original iterator.

Description

This method exists to convert custom iterators, probably exported by libraries, to proper iterators. All iterator objects returned by Iterator.from() inherit from a common prototype object, which has the following methods:

next()

Calls the underlying iterator's next() method and returns the result.

return()

Calls the underlying iterator's return() method and returns the result, or returns { value: undefined, done: true } if the underlying iterator doesn't have a return() method.

Examples

Converting an iterable to a proper iterator

Because obj is already an iterable that returns a proper iterator when its [Symbol.iterator]() method is called, Iterator.from(obj) returns the same iterator.

js
const iterator = (function* () {
  yield 1;
  yield 2;
  yield 3;
})();

const obj = {
  [Symbol.iterator]() {
    return iterator;
  },
};

const iterator2 = Iterator.from(obj);
console.log(iterator2 === iterator); // true

Because obj2 is an iterable that returns a non-proper iterator when its [Symbol.iterator]() method is called, Iterator.from(obj2) returns a new iterator that wraps the original iterator.

js
const iterator = {
  current: 0,
  next() {
    return { value: this.current++, done: false };
  },
};

const obj2 = {
  [Symbol.iterator]() {
    return iterator;
  },
};

const iterator2 = Iterator.from(obj2);
console.log(iterator2 === iterator); // false
console.log(iterator2.next()); // { value: 0, done: false }
console.log(iterator.next()); // { value: 1, done: false }

Converting an iterator to a proper iterator

Because obj is already a proper iterator, Iterator.from(obj) returns itself.

js
const obj = (function* () {
  yield 1;
  yield 2;
  yield 3;
})();

const iterator = Iterator.from(obj);
console.log(iterator === obj); // true

Because obj2 is a non-proper iterator, Iterator.from(obj2) returns a new iterator that wraps the original iterator.

js
const obj2 = {
  current: 0,
  next() {
    return { value: this.current++, done: false };
  },
};

const iterator = Iterator.from(obj2);
console.log(iterator === obj2); // false
console.log(iterator.next()); // { value: 0, done: false }
console.log(obj2.next()); // { value: 1, done: false }

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-iterator.from

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
from
Chrome – No support
Chrome 117 – 118 (Release date: 2023-09-12)
footnote Removed in 119 and later
Chrome – Full support
Chrome 122 (Release date: 2024-02-20)
footnote Full support
Edge – No support
Edge 117 – 118 (Release date: 2023-09-15)
footnote Removed in 119 and later
Edge – Full support
Edge 122 (Release date: 2024-02-23)
footnote Full support
Firefox – Full support
Firefox 131 (Release date: 2024-10-01)
footnote Full support
Opera – No support
Opera 103 – 104 (Release date: 2023-10-03)
footnote Removed in 105 and later
Opera – Full support
Opera 108 (Release date: 2024-03-05)
footnote Full support
Safari – Full support
Safari 18.4 (Release date: 2025-03-31)
footnote Full support
Chrome Android – No support
Chrome Android 117 – 118 (Release date: 2023-09-12)
footnote Removed in 119 and later
Chrome Android – Full support
Chrome Android 122 (Release date: 2024-02-20)
footnote Full support
Firefox for Android – Full support
Firefox for Android 131 (Release date: 2024-10-01)
footnote Full support
Opera Android – No support
Opera Android 78 – 78 (Release date: 2023-10-23)
footnote Removed in 79 and later
Opera Android – Full support
Opera Android 81 (Release date: 2024-03-14)
footnote Full support
Safari on iOS – Full support
Safari on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
Samsung Internet – No support
Samsung Internet 24 – 24 (Release date: 2024-01-25)
footnote Removed in 25 and later
Samsung Internet – Full support
Samsung Internet 26 (Release date: 2024-06-07)
footnote Full support
WebView Android – No support
WebView Android 117 – 118 (Release date: 2023-09-12)
footnote Removed in 119 and later
WebView Android – Full support
WebView Android 122 (Release date: 2024-02-20)
footnote Full support
WebView on iOS – Full support
WebView on iOS 18.4 (Release date: 2025-03-31)
footnote Full support
Bun – Full support
Bun 1.1.31 (Release date: 2024-10-18)
footnote Full support
Deno – Full support
Deno 1.39 (Release date: 2023-12-14)
footnote Full support
Node.js – Full support
Node.js 22 (Release date: 2024-04-24)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Has more compatibility info.

See also