What is the difference between Array.find() and Array.filter()?
Answer
Both search an array using a predicate function, but they differ in what they return. find(callback) returns the first element that satisfies the condition — it stops searching as soon as it finds a match (early termination for performance). If no element matches, returns undefined. users.find(u => u.id === 5) returns the user object or undefined. Use when you need a single matching item and know only one (or the first) matters. filter(callback) returns a new array of ALL elements that satisfy the condition — it always processes every element. Returns an empty array if nothing matches. users.filter(u => u.active) returns all active users. Use when you need all matching items. Related: findIndex() returns the index of the first match (or -1); findLast() / findLastIndex() (ES2023) search from the end. Performance tip: find() is more efficient when you only need the first match — filter()[0] processes the entire array unnecessarily.