Table of Contents
The Short Answer
filter() returns all matches: the result is always an array, even when only one item matches or none do.
find() returns the first match: the result is the item itself, or undefined when nothing matches.
let numbers = [5, 12, 8, 20, 3]; console.log(numbers.filter(n => n > 10)); // [12, 20] console.log(numbers.find(n => n > 10)); // 12
What filter() Does
Checks every item: it never stops early, because it needs to find all the matches.
Returns a new array: it contains only the items that passed the test.
Returns an empty array when nothing matches: it never returns undefined.
Leaves the original array alone: nothing is changed.
let products = [
{ name: "Laptop", inStock: true },
{ name: "Mouse", inStock: false },
{ name: "Keyboard", inStock: true }
];
let available = products.filter(p => p.inStock);
console.log(available.length); // 2
What find() Does
Stops at the first match: the rest of the array is never checked.
Returns the item itself: it is not wrapped in an array.
Returns undefined when nothing matches: so you should check the result before using it.
Leaves the original array alone: nothing is changed.
let firstAvailable = products.find(p => p.inStock);
console.log(firstAvailable); // { name: "Laptop", inStock: true }
Same Data, Different Results
Searching for a user by ID:
let users = [
{ id: 1, name: "Vi" },
{ id: 2, name: "Sam" },
{ id: 3, name: "Alex" }
];
let withFilter = users.filter(u => u.id === 2);
console.log(withFilter); // [{ id: 2, name: "Sam" }]
console.log(withFilter[0]); // { id: 2, name: "Sam" }, you need [0] to reach the object
let withFind = users.find(u => u.id === 2);
console.log(withFind); // { id: 2, name: "Sam" }, the object directly
When nothing matches:
console.log(users.filter(u => u.id === 99)); // [] console.log(users.find(u => u.id === 99)); // undefinedWhy this matters: an empty array is truthy, but undefined is falsy, so the two need different checks.
let result1 = users.filter(u => u.id === 99);
if (result1) {
console.log("This runs, even though nothing matched"); // a common bug
}
let result2 = users.find(u => u.id === 99);
if (result2) {
console.log("This does not run");
}
The correct check for filter(): look at the length instead.
if (result1.length > 0) {
console.log("Found at least one match");
}
When to Use filter()
You need every match: for example, all products under a certain price.let cheap = products.filter(p => p.inStock);You need to remove items: keep everything except what you want gone.
let tasks = ["buy milk", "study", "gym"]; let remaining = tasks.filter(task => task !== "study"); console.log(remaining); // ["buy milk", "gym"]You want to count matches: use the length of the result.
let passed = [45, 80, 62, 30].filter(score => score >= 50).length; console.log(passed); // 2You plan to chain other methods: the result is an array, so map() and others can follow.
let names = users.filter(u => u.id > 1).map(u => u.name); console.log(names); // ["Sam", "Alex"]
When to Use find()
You need one specific item: such as a user with a particular ID.
You only care about the first match: and the rest are not important.
Performance matters on large arrays: find() stops as soon as it has an answer.
You want to use the item straight away: without reaching into an array.
let user = users.find(u => u.id === 2);
if (user) {
console.log(user.name); // "Sam"
}
What About findIndex() and some()?
findIndex(): works like find(), but returns the position instead of the item, or -1 when nothing matches.
some(): returns only true or false, which is ideal when you just need to know whether a match exists.
let nums = [3, 7, 12, 9]; console.log(nums.find(n => n > 10)); // 12 console.log(nums.findIndex(n => n > 10)); // 2 console.log(nums.some(n => n > 10)); // true console.log(nums.filter(n => n > 10)); // [12]
| Method | Returns |
| filter() | All matching items in an array |
| find() | The first matching item |
| findIndex() | The position of the first match |
| some() | true or false |
Common Mistakes
Using filter() when you only need one item: you end up writing [0] everywhere, and you check more items than necessary.
Checking a filter() result with a simple if: an empty array is truthy, so check its length instead.
Using find() when you need every match: it returns only the first one.
Forgetting that find() can return undefined: reading a property from it throws an error.
Using filter() just to check existence: some() is clearer and stops early.
Conclusion
Frequently Asked Questions (FAQs)
2.What does find() return if there are several matches?find() is usually faster, because it stops as soon as it finds a match. filter() always checks every item.
3.Can I use filter() to get a single item?Only the first one. The rest are ignored.
Yes, by taking the first element of the result, as in array.filter(condition)[0]. However, find() is simpler and faster for that job.
0 Comments