Array.prototype.find
Returns the first element in the provided array that satisfies the provided testing function
Syntax
array.find(callbackFn, thisArg?)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | (element, index, array) => boolean | Function to test each element |
| thisArg | any | Value to use as this when executing callbackFn |
Return Value
The first element that satisfies the testing function, or undefined
Examples
const numbers = [5, 12, 8, 130, 44];
const found = numbers.find(n => n > 10);
console.log(found); // 12const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
];
const user = users.find(u => u.id === 2);
console.log(user?.name); // 'Bob'const inventory = [
{ name: 'apples', qty: 2 },
{ name: 'bananas', qty: 0 },
{ name: 'cherries', qty: 5 }
];
const inStock = inventory.find(item => item.qty > 0);
console.log(inStock?.name); // 'apples'Understanding Array.prototype.find
The Array.prototype.find method in JavaScript returns the first element in the provided array that satisfies the provided testing function. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.
The method signature is array.find(callbackFn, thisArg?). It accepts 2 parameters: callbackFn, thisArg. When called, it returns the first element that satisfies the testing function, or undefined. Understanding when and how to use find() helps you write more expressive, readable code.
Common use cases for Array.prototype.find include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-findindex, array-findlast, array-filter, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Array.prototype.find is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Array.prototype.findIndexReturns the index of the first element in an array that satisfies the provided testing function
Array.prototype.findLastIterates the array in reverse order and returns the value of the first element that satisfies the provided testing function
Array.prototype.filterCreates a shallow copy of a portion of a given array, filtered down to just the elements that pass the test implemented by the provided function
Array.prototype.includesDetermines whether an array includes a certain value among its entries, returning true or false
More Array Methods
Other methods in the Array object
Related Tools
More Array Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.