JavaScript filter Method
Table of Contents + β
In the previous lesson, we learned how map() transforms every item in an array. Now letβs learn filter(), which selects only the items you want to keep.
π What is the filter Method?
The filter() method creates a new array containing only the items that pass a test. You give it a callback function, and that callback runs once for each item. When the callback returns true, the item is kept. When it returns false, the item is dropped.
const numbers = [1, 2, 3, 4, 5, 6];
const evens = numbers.filter((number) => number % 2 === 0);
console.log(evens); // [2, 4, 6]console.log(numbers); // [1, 2, 3, 4, 5, 6]The callback (number) => number % 2 === 0 returns true only for even numbers, so filter() keeps just 2, 4, and 6. The original numbers array stays exactly the same, because filter() does not change the array it runs on.
π filter vs map
These two methods look similar, but they do different jobs. map() transforms every item and always returns an array of the same length. filter() selects some items and returns an array that is the same length or shorter.
| Method | What it does | Result length |
map() | Transforms all items | Same as the original |
filter() | Selects some items | Same or shorter |
The key difference is what the callback returns. With map(), the callback returns a new value for each item. With filter(), the callback returns true or false to decide whether to keep each item.
π A Practical Example
Filtering really shines with arrays of objects. Here we keep only the products that cost less than 50.
const products = [ { name: "Notebook", price: 12 }, { name: "Headphones", price: 80 }, { name: "Pen", price: 3 }, { name: "Monitor", price: 150 },];
const affordable = products.filter((product) => product.price < 50);
console.log(affordable);// [ { name: "Notebook", price: 12 }, { name: "Pen", price: 3 } ]The callback checks product.price < 50 for each product. Only the two products that pass the test end up in the new affordable array. The same idea works for keeping active users.
const users = [ { name: "Alex", active: true }, { name: "Sam", active: false }, { name: "Jordan", active: true },];
const activeUsers = users.filter((user) => user.active);
console.log(activeUsers);// [ { name: "Alex", active: true }, { name: "Jordan", active: true } ]Letβs walk through what each line does:
- We start with three user objects, each carrying an
activeflag that istrueorfalse. - The callback
(user) => user.activereturns that flag directly, sofilter()keeps the user wheneveractiveistrue. - Alex and Jordan pass, while Sam is dropped, so
activeUsersholds only the two active users.
π When Nothing Matches
If no item passes the test, filter() returns an empty array. It never returns undefined or null, so you can safely loop over the result without checking first.
const numbers = [1, 2, 3];
const bigNumbers = numbers.filter((number) => number > 100);
console.log(bigNumbers); // []console.log(bigNumbers.length); // 0Here is what happens line by line:
- The callback
(number) => number > 100is checked against1,2, and3, and none of them clear100. - Since nothing passes,
filter()hands back an empty array[]rather thanundefinedornull. - Reading
bigNumbers.lengthconfirms it is a real array with0items, which is why it is still safe to loop over.
Always an array
Because filter() always returns an array, you can chain it with other array
methods like map() right away. An empty array is still an array.
β οΈ Common Mistakes to Avoid
| Mistake | Problem | Solution |
| Returning a value instead of a condition | filter((n) => n * 2) keeps almost everything | Return a boolean: filter((n) => n > 2) |
| Expecting a single item | filter() always returns an array, not one value | Use find() when you want a single match |
| Thinking it changes the original | The source array is never modified | Store the returned array in a new variable |
Return a true or false
The callback must return a boolean condition. A returned number or string is treated as truthy or falsy, which can keep items you did not expect.
π§ Try It Yourself!
- Create an array of numbers and use
filter()to keep only the values greater than10. - Make an array of product objects and filter out the ones above a certain price.
- Filter an array of users to keep only the ones where
activeistrue. - Filter for something that does not exist and confirm you get an empty array.
π§© What Youβve Learned
- β
filter()creates a new array of items that pass a test - β
The callback returns
trueto keep an item andfalseto drop it - β
map()transforms all items, whilefilter()selects some - β
filter()never changes the original array - β It returns an empty array when nothing matches
π Whatβs Next?
Sometimes you want just one matching item, not a whole array. Letβs continue to find.