On this page
Array.find
Prev: reduce | Up: Array | Next: flatMap
Rel: Array, Array & Typed Arrays Guide
Ref: MDN Web Docs — Array.find
Tags: #public #web #javascript #builtins #array #tier/core
Signature & Overview
arr.find(predicate)
Returns the first element matching predicate, or undefined (short-circuits on first match).
Notes & Pitfalls
- Short-circuits on the first match: Returns
undefinedif nothing matches (usefindIndex()ifundefinedcan be a valid element). - Use
findLast()(ES2023) to search from the end.
Example
const users = [{ id: 1, name: "Ada" }, { id: 2, name: "Grace" }];
const found = users.find((u) => u.id === 2);
console.assert(found?.name === "Grace");
Linked from
Related notes
- tuts/javascript/builtins/Array/flatMap Linked here; links to this page; shared tags: #array, #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/reduce Linked here; links to this page; shared tags: #array, #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array Linked here; links to this page; shared tags: #array, #builtins, #javascript, #web
- tuts/javascript/builtins/array_and_typed_arrays Linked here; shared tags: #builtins, #javascript, #web
- tuts/javascript/builtins/Array/at shared tags: #array, #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/filter shared tags: #array, #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/from shared tags: #array, #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/includes shared tags: #array, #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/isArray shared tags: #array, #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/map shared tags: #array, #builtins, #javascript, #tier, #web