How does find works in javscript? 1

How does find works in javscript?

const products = [
    { name: 'Laptop', price: 32000, brand: 'Lenovo', color: 'Silver' },
    { name: 'Phone', price: 700, brand: 'Iphone', color: 'Golden' },
    { name: 'Watch', price: 3000, brand: 'Casio', color: 'Yellow' },
    { name: 'Aunglass', price: 300, brand: 'Ribon', color: 'Blue' },
    { name: 'Camera', price: 9000, brand: 'Lenovo', color: 'Gray' },
];
//Get product that price is greater than 3000 by using a find
const getProduct = products.find(product => product.price > 3000);
console.log(getProduct) //first conditon will be print
//Expected output:
/* { name: 'Laptop', price: 32000, brand: 'Lenovo', color: 'Silver' } */

Here is what the above code is Doing:
1. We have an array of products.
2. We are using the find method to find the product that has a price greater than 3000.
3. The find method will return the first product that matches the condition.
4. The find method will return undefined if no product matches the condition.

Similar Posts