Tutorial

Using the Array.find Method in JavaScript

Practical guide to the JavaScript Array.find method with examples, comparisons, and tips.

Drake Nguyen

Founder · System Architect

3 min read
Using the Array.find Method in JavaScript
Using the Array.find Method in JavaScript

When to use the JavaScript Array find method

The JavaScript Array find method (Array.prototype.find) is designed to return the first element in an array that satisfies a predicate function. Use it when you need the first match — not a list of matches. If you expect multiple results, prefer Array.filter instead.

Quick points:

  • Returns the first element that passes the test.
  • Returns undefined when no element is found.
  • Accepts a callback (predicate) that receives element, index and the array.

How to use Array.prototype.find

The only required parameter is a callback function that tests each element. The callback signature is (element, index, array) => boolean. When the callback returns true for an element, find immediately returns that element.

Simple example

const numbers = [10, 20, 30, 40];
const firstOver25 = numbers.find(n => n > 25);
// firstOver25 === 30

Find an object by property (JavaScript find object in array by property)

const users = [
  { id: 1, name: 'Ava' },
  { id: 2, name: 'Noah' },
  { id: 3, name: 'Liam' }
];
const user = users.find(u => u.id === 2);
// { id: 2, name: 'Noah' }

This is the common approach to find element in array javascript when working with arrays of objects.

Reusing the predicate function

If the same test is used in multiple places, extract it into a named function for clarity and reuse.

const hasEnough = item => item.count >= 5;
const arr1 = [{count:4},{count:6}];
const arr2 = [{count:2},{count:5}];
arr1.find(hasEnough); // returns {count:6}
arr2.find(hasEnough); // returns {count:5}

Using the index parameter (Array.find with index parameter JavaScript)

The callback receives the current index as the second argument. Use it when position matters—for example to skip the first element.

const items = ['a','b','c'];
const result = items.find((el, i) => i !== 0 && el === 'b');
// result === 'b'

Behavior details and comparisons

Important behaviors to remember:

  • Undefined when not found: Array.find returns undefined if nothing passes the predicate (Array.find returns undefined).
  • Stops early: find returns on the first match — efficient when you only need the first match.
  • Difference from filter: filter returns all matching elements as a new array (array find vs filter).
  • findIndex: If you need the index of the match rather than the value, use findIndex() (Array.find findIndex difference).

Polyfill note

Older environments may not include Array.prototype.find. A polyfill implementing the same callback behavior and return semantics is available from standards-based sources if you must support legacy browsers (Array.prototype.find polyfill).

Tips and gotchas

  • Netalith not mutate the array inside the predicate — it can lead to unpredictable results.
  • If you need to check multiple conditions, make the predicate expressive and well-named.
  • Remember the predicate parameters: element, index, array (callback parameters element index array).

Conclusion

The JavaScript find() method is a concise way to find the first matching element in an array. Use Array.find when you expect a single result, use filter when you need many, and use findIndex when you need the position. Combined with clear predicate functions, Array.prototype.find is one of the most useful ES6 array methods for everyday JavaScript work.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.