Unlike filter method that returns all the matching elements, find and findIndex will stop at the first matching element found.
The find method returns the first element in an array that satisfies a given test function. If no elements satisfy the test, it returns undefined.
const numbers = [10, 20, 30, 40]; const found = numbers.find(number => number > 20); console.log(found); // 30
The findIndex method returns the index of the first element in an array that satisfies a given test function. If no elements satisfy the test, it returns -1.
const numbers = [10, 20, 30, 40]; const index = numbers.findIndex(number => number > 20); console.log(index); // 2