Master programming syntax, solve interactive challenges & run live code in Syntaxis Explore Arena →
Reference / JavaScript

Array.prototype.find()

Returns the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.

Syntax
find(predicate: (element: T, index: number, array: T[]) => boolean): T | undefined

Parameters

NameTypeDescription
predicateFunctionFunction executed for each value.

Examples

Find by ID
const users = [{ id: 10, name: 'Alice' }, { id: 20, name: 'Bob' }];
const found = users.find(u => u.id === 20);
console.log(found); // { id: 20, name: 'Bob' }
Stops iterating as soon as a matching element is found (short-circuits).