The find() method in Javascript is used to find an element in an array. The method returns the first element that satisfies the given condition in the callback function. It returns undefined if no element is found matching the criteria.

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

let search = numbers.find(function (n) {
    return n == 3;
});

console.log(search); // 3
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];

let search = numbers.find(function (n) {
    return n == 10;
});

console.log(search); // undefined

Using index in find() method

The second argument of the callback function passed to the find() method is index.

let numbers = [5, 6, 7, 8, 9];

let search = numbers.find(function (n, i) {
    console.log(i);
    return n == 8;
});

console.log(search); 

/*
OUTPUT:
0
1
2
3
8
*/

Find an object using find() method

The find() method can also be helpful in finding an object in an array of objects.

let orders = [
    {
        id: 1,
        product: 'Jeans',
        country: 'United States'
    },
    {
        id: 2,
        product: 'T-shirt',
        country: 'United States'
    },
    {
        id: 3,
        product: 'Shoes',
        country: 'United States'
    }
];

let search = orders.find(function (order) {
    return order.product == "Jeans";
});

console.log(search); // {id: 1, product: 'Jeans', country: 'United States'}

The find() method’s callback function uses the third argument to hold the array on which the method was invoked.

let numbers = [1, 2, 3, 4, 5];

let search = numbers.find(function (value, index, arr) { 
    console.log(arr);
    return value === 5;
});

/*
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
*/