• Skip to main content
  • Skip to search
  • Skip to select language
HTML

Structure of content on the web

  • Web APIs

    Interfaces for building web applications

  • Learn
    • CSS

      Learn to style content using CSS

    • Overview

      A customized MDN experience

    • FAQ

      Frequently asked questions about MDN Plus

  • HTTP Observatory

    Scan a website for free

  • JavaScript
  • toArray()
      • Deutsch
      • 日本語
      • 한국어
      • 中文 (简体)

    In this article

    • Syntax
    • Examples
    • Specifications
    • Browser compatibility
    • See also
    1. from()
  • Instance methods
    1. find()
    2. reduce()
    3. [Symbol.iterator]()
  • Inheritance
  • call()
  • displayName Non-standard
  • arguments Non-standard Deprecated
  • caller Non-standard Deprecated
  • Instance methods
    1. __defineGetter__() Deprecated
    2. __defineSetter__() Deprecated
    3. __lookupGetter__() Deprecated
    4. __lookupSetter__() Deprecated
    5. toLocaleString()
    6. __proto__ Deprecated
    7. Array instance populated with the elements yielded from the iterator.

  • Syntax

    js
    toArray()
    

    Parameters

    None.

    Return value

    A new Array instance containing the elements from the iterator in the order they were produced.

    Examples

    Using toArray()

    iterator.toArray() is equivalent to Array.from(iterator) and [...iterator], except that it's easier to chain when multiple iterator helper methods are involved. The following example creates an iterator that yields terms in the Fibonacci sequence, takes the first 10 terms, filters out the odd numbers, and converts the result to an array:

    js
    function* fibonacci() {
      let current = 1;
      let next = 1;
      while (true) {
        yield current;
        [current, next] = [next, current + next];
      }
    }
    
    const array = fibonacci()
      .take(10)
      .filter((x) => x % 2 === 0)
      .toArray();
    
    console.log(array); / [2, 8, 34]
    

    Note that it's a good idea to call toArray() as a last step of your processing. For example, fibonacci().take(10).toArray().filter(...) is less efficient, because iterator helpers are lazy and avoids creating a temporary array.

    Specifications

    Specification
    Iterator Helpers
    # sec-iteratorprototype.toarray

    Browser compatibility

    See also

    • Polyfill of Iterator.prototype.toArray in core-js
    • es-shims polyfill of Iterator.prototype.toArray
    • Iterator
    • Array.from()

    Help improve MDN

    Iterator object is an abstract base for objects that implement the iterator protocol. It provides methods common to built-in iterators, such as filter(), find(), map(), and reduce(). You can also use the static method Iterator.from() to convert an existing iterable into an Iterator.","name":"Iterator methods"}},"browserCompat":["javascript.builtins.Iterator.toArray"],"pageType":"javascript-instance-method"}}

    Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

    Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant