• 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
  • map()
      • Deutsch
      • 日本語
      • 中文 (简体)

    In this article

    • Syntax
    • Description
    • 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. iterator helper object that yields elements of the iterator, each transformed by a mapping function.

  • Syntax

    js
    map(callbackFn)
    

    Parameters

    callbackFn

    A function to execute for each element produced by the iterator. Its return value is yielded by the iterator helper. The function is called with the following arguments:

    element

    The current element being processed.

    index

    The index of the current element being processed.

    Return value

    A new iterator helper object. Each time the iterator helper's next() method is called, it gets the next element from the underlying iterator, applies callbackFn, and yields the return value. When the underlying iterator is completed, the iterator helper is also completed (the next() method produces { value: undefined, done: true }).

    Description

    The main advantage of iterator helpers over array methods is that they are lazy, meaning that they only produce the next value when requested. This avoids unnecessary computation and also allows them to be used with infinite iterators. The map() method allows you to create a new iterator that, when iterated, produces transformed elements.

    Examples

    Using map()

    The following example creates an iterator that yields terms in the Fibonacci sequence, transforms it into a new sequence with each term squared, and then reads the first few terms:

    js
    function* fibonacci() {
      let current = 1;
      let next = 1;
      while (true) {
        yield current;
        [current, next] = [next, current + next];
      }
    }
    
    const seq = fibonacci().map((x) => x ** 2);
    console.log(seq.next().value); / 1
    console.log(seq.next().value); / 4
    

    Using map() with a for...of loop

    map() is most convenient when you are not hand-rolling the iterator. Because iterators are also iterable, you can iterate the returned helper with a for...of loop:

    js
    for (const n of fibonacci().map((x) => x ** 2)) {
      console.log(n);
      if (n > 30) {
        break;
      }
    }
    
    / Logs:
    / 1
    / 4
    / 9
    / 25
    / 64
    

    This is equivalent to:

    js
    for (const n of fibonacci()) {
      const n2 = n ** 2;
      console.log(n2);
      if (n2 > 30) {
        break;
      }
    }
    

    Specifications

    Specification
    Iterator Helpers
    # sec-iteratorprototype.map

    Browser compatibility

    See also

    • Polyfill of Iterator.prototype.map in core-js
    • es-shims polyfill of Iterator.prototype.map
    • Iterator
    • Iterator.prototype.flatMap()
    • Array.prototype.reduce()

    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.map"],"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