• 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
  • reduce()
      • 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. Array.prototype.reduce: it executes a user-supplied "reducer" callback function on each element produced by the iterator, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements is a single value.

  • Syntax

    js
    reduce(callbackFn)
    reduce(callbackFn, initialValue)
    

    Parameters

    callbackFn

    A function to execute for each element produced by the iterator. Its return value becomes the value of the accumulator parameter on the next invocation of callbackFn. For the last invocation, the return value becomes the return value of reduce(). The function is called with the following arguments:

    accumulator

    The value resulting from the previous call to callbackFn. On the first call, its value is initialValue if the latter is specified; otherwise its value is the first element of the iterator.

    currentValue

    The value of the current element. On the first call, its value is the first element of the iterator if initialValue is specified; otherwise its value is the second element.

    currentIndex

    The index position of currentValue. On the first call, its value is 0 if initialValue is specified, otherwise 1.

    initialValue Optional

    A value to which accumulator is initialized the first time the callback is called. If initialValue is specified, callbackFn starts executing with the first element as currentValue. If initialValue is not specified, accumulator is initialized to the first element, and callbackFn starts executing with the second element as currentValue. In this case, if the iterator is empty (so that there's no first value to return as accumulator), an error is thrown.

    Return value

    The value that results from running the "reducer" callback function to completion over the entire iterator.

    Exceptions

    TypeError

    Thrown if the iterator contains no elements and initialValue is not provided.

    Description

    See Array.prototype.reduce() for details about how reduce() works. Unlike most other iterator helper methods, it does not work well with infinite iterators, because it is not lazy.

    Examples

    Using reduce()

    The following example creates an iterator that yields terms in the Fibonacci sequence, and then sums the first ten terms:

    js
    function* fibonacci() {
      let current = 1;
      let next = 1;
      while (true) {
        yield current;
        [current, next] = [next, current + next];
      }
    }
    
    console.log(
      fibonacci()
        .take(10)
        .reduce((a, b) => a + b),
    ); / 143
    

    Specifications

    Specification
    Iterator Helpers
    # sec-iteratorprototype.reduce

    Browser compatibility

    See also

    • Polyfill of Iterator.prototype.reduce in core-js
    • es-shims polyfill of Iterator.prototype.reduce
    • Iterator
    • Iterator.prototype.map()
    • 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.reduce"],"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