Table of Contents
What find() Does
Returns the first match: it checks items in order and gives you the first one that passes your condition.
Stops as soon as it finds it: the rest of the array never gets checked.
Returns the item itself: not an array, not an index, just the actual value or object.
Leaves the original array alone: it only reads through it.
let numbers = [5, 12, 8, 20, 3]; let firstOverTen = numbers.find(n => n > 10); console.log(firstOverTen); // 12
Syntax and Parameters
array.find(function (item, index, array) {
return condition;
});
item: the current element being checked.
index: its position in the array.
array: the full array. You'll rarely need it.
The function must return true or false: that decides whether the item counts as a match.
Practical Examples
Finding an object by ID:
let users = [
{ id: 1, name: "Vi" },
{ id: 2, name: "Sam" },
{ id: 3, name: "Alex" }
];
let user = users.find(u => u.id === 2);
console.log(user); // { id: 2, name: "Sam" }
Finding the first item that meets a condition:
let products = [
{ name: "Laptop", inStock: false },
{ name: "Mouse", inStock: true },
{ name: "Keyboard", inStock: true }
];
let available = products.find(p => p.inStock);
console.log(available); // { name: "Mouse", inStock: true }
Finding a string:
let words = ["apple", "banana", "cherry"];
console.log(words.find(w => w.startsWith("b"))); // "banana"
Things to Remember
No match gives you undefined: it won't throw an error, it just returns undefined.let result = [1, 2, 3].find(n => n > 100); console.log(result); // undefinedCheck before using the result: calling a property on undefined throws an error.
let user = users.find(u => u.id === 99);
if (user) {
console.log(user.name);
} else {
console.log("User not found");
}
Only the first match counts: if several items pass, you still only get one.
It can't tell you the position: for that, use findIndex().
Common Mistakes
Expecting an array back: find() gives you one item, not a list. Use filter() if you want all matches.
Skipping the undefined check: this is the most common cause of "cannot read properties of undefined" errors.
Forgetting to return a value: with curly braces, you need an explicit return, otherwise nothing ever matches.
let broken = [1, 2, 3].find(n => { n > 1 }); // undefined, no return
let fixed = [1, 2, 3].find(n => { return n > 1; }); // 2
Conclusion
find() does one job well: it grabs the first item that matches a condition and stops. Remember that it returns undefined when nothing matches, and check for that before using the result, and you'll avoid most of the problems people run into with it.Frequently Asked Questions (FAQs)
1.What's the difference between find() and filter()?2.What does find() return if nothing matches?find() returns the first matching item. filter() returns every matching item in a new array.
3.How do I get the position instead of the item?undefined. It doesn't throw an error.
Use findIndex(). It works the same way but returns the index, or -1 if nothing matches.
0 Comments