liveBook · Manning

Find

As explained in Secrets of the JavaScript Ninja, Second Edition

An Array method that returns the first element passing a callback test, or undefined if none does.

Array `find` tests items with a callback and returns the first item that passes. If no item passes, it returns `undefined`; use `filter` when you need all matches. section 9.1.4

Find a ninja by weapon

const ninjaWithWakizashi = ninjas.find(ninja => ninja.weapon === "wakizashi"); // Returns Kuma's ninja object. section 9.1.4

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Explains callback-based matching and the `undefined` no-match result, without detailing implementation internals.
Examples
Finds the ninja wielding a wakizashi and checks for a katana match that yields `undefined`.
What you can do after
Apply `find` with a callback to retrieve the first matching array item or detect that none matches.
How it connects
Connects callback predicates to `filter`, which returns all matching items rather than just the first.

Also explained in

Compare how 4 books explain find →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.