How do find() and findIndex() differ from filter()?
Short answer
find() returns the first matching element (or undefined); findIndex() returns its index (or -1); filter() returns ALL matching elements as a new array, even if there's only one match.
Advertisement
AModel answer
find() returns the first matching element (or undefined); findIndex() returns its index (or -1); filter() returns ALL matching elements as a new array, even if there's only one match.
[5,12,8].find(n => n > 10); // 12Real-world example
Finding a specific user by ID in a list, rather than filtering the whole array.
Likely follow-ups
- How would you find the LAST matching element instead of the first?
Related topics