// .find method only accepts a callback function
// and returns the first value for which the function is true.
const cities = [
"Orlando",
"Dubai",
"Denver",
"Edinburgh",
"Chennai",
"Accra",
"Denver",
"Eskisehir",
];
const findCity = (element) => {
return element === "Denver";
};
console.log(cities.find(findCity));
//Expected output is 4 (first instance of "Denver" in array)
The find() method returns the value of the first element in the provided array that satisfies the provided testing function. If no values satisfy the testing function, undefined is returned.
//find is higher oder function that return only if condition is true
const names= ["Shirshak","SabTech","Kdc"]
const searchName = names.find(function(name){
return names.inclues("Shirshak")
})
str.indexOf("locate"); // return location of first find value
str.lastIndexOf("locate"); // return location of last find value
str.indexOf("locate", 15); // start search from location 15 and then take first find value
str.search("locate");
//The search() method cannot take a second start position argument.
//The indexOf() method cannot take powerful search values (regular expressions).
/* This method returns first element of the array that satisfies the condition
specified in the callback function.
*/
const x = [1, 2, 3, 4, 5];
const y = x.find(el => el*2 === 2);
console.log("y is: ", y); // y is: 1
/* Now, if you see the output of the above example, the value of y is 1.
This is because the find() method searches for first element in the array
that satisfies the condition specified.
*/