What is the difference between find and filter?
Both search an array with a predicate, but they answer different questions and return different things. Find returns the first element that matches, or the empty value if nothing matches. Filter returns a new array containing every element that matches, which may be empty.
The distinction matters for readability. If you write filter and then take the first element of the result, you are expressing a single-item lookup in a way that hides the intent and costs extra work. The reverse mistake, using find when you actually need all the matches, is worse, because it silently drops data.
Both accept the same predicate signature, so the callback receives the element, the index and the whole array. Both skip holes in a sparse array. And both leave the original array untouched, returning new data rather than mutating.
There is a third option worth knowing. FindIndex returns the position of the first match rather than the element, which is what you want when the element itself is not enough, for example when you need to remove it or highlight it in a list. FindLast searches from the end, which is handy for picking the most recent entry in a log, and findLastIndex does the same for positions. Both of those arrived in the 2023 edition of the language and are available in current Node and browsers.