• 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
  • find()
      • 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.find(): it returns the first element produced by the iterator that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

  • Syntax

    js
    find(callbackFn)
    

    Parameters

    callbackFn

    A function to execute for each element produced by the iterator. It should return a falsy value otherwise. 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

    The first element produced by the iterator that satisfies the provided testing function. Otherwise, undefined is returned.

    Description

    find() iterates the iterator and invokes the callbackFn function once for each element. It returns the element immediately if the callback function returns a truthy value. Otherwise, it iterates until the end of the iterator and returns undefined. If find() returns an element, the underlying iterator is closed by calling its return() method.

    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. With infinite iterators, find() returns the first satisfying element as soon as it is found. If the callbackFn always returns a falsy value, the method never returns.

    Examples

    Using find()

    js
    function* fibonacci() {
      let current = 1;
      let next = 1;
      while (true) {
        yield current;
        [current, next] = [next, current + next];
      }
    }
    
    const isEven = (x) => x % 2 === 0;
    console.log(fibonacci().find(isEven)); / 2
    
    const isNegative = (x) => x < 0;
    console.log(fibonacci().take(10).find(isNegative)); / undefined
    console.log(fibonacci().find(isNegative)); / Never completes
    

    Calling find() always closes the underlying iterator, even if the method early-returns. The iterator is never left in a half-way state.

    js
    const seq = fibonacci();
    console.log(seq.find(isEven)); / 2
    console.log(seq.next()); / { value: undefined, done: true }
    

    Specifications

    Specification
    Iterator Helpers
    # sec-iteratorprototype.find

    Browser compatibility

    See also

    • Polyfill of Iterator.prototype.find in core-js
    • es-shims polyfill of Iterator.prototype.find
    • Iterator
    • Iterator.prototype.every()
    • Iterator.prototype.some()
    • Array.prototype.find()

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