Last modified: Jul 27, 2026
JavaScript Filter Array Guide
Working with arrays is a core part of JavaScript. You often need to pick specific items from a list. The filter() method is your best tool for this task. It creates a new array with only the elements that pass a test. This guide will show you how to use it well.
What is the Filter Method?
The filter() method loops through an array. It checks each item against a condition. If the item is true, it stays. If false, it is removed. The original array never changes. You get a fresh array with the matching items.
Think of it like a sieve. You pour all your data in. Only the pieces that fit through the holes end up in your new collection. This makes filter() safe and predictable.
Basic Syntax
The syntax is simple. You call filter() on an array. You pass a callback function inside. This function runs for every element.
// Basic structure
let newArray = originalArray.filter(callbackFunction);
The callback function takes three arguments. The first is the current element. The second is the index. The third is the original array. You usually only need the first one.
// Callback arguments
array.filter(function(element, index, array) {
// Return true to keep the element
// Return false to discard it
});
Simple Filter Example
Let's start with a basic example. We have an array of numbers. We want only numbers greater than 10.
let numbers = [5, 12, 8, 20, 3, 15];
// Filter numbers greater than 10
let bigNumbers = numbers.filter(function(num) {
return num > 10;
});
console.log(bigNumbers);
[12, 20, 15]
Notice how 5, 8, and 3 are gone. The new array only has 12, 20, and 15. The original numbers array stays unchanged.
Filter with Arrow Functions
Arrow functions make the code shorter and cleaner. They are perfect for simple conditions.
let scores = [45, 78, 32, 90, 55];
// Keep scores that are 50 or above
let passing = scores.filter(score => score >= 50);
console.log(passing);
[78, 90, 55]
The arrow function score => score >= 50 does the same job as a longer function. It is easier to read and write.
Filtering Strings
You can filter arrays of strings too. This is great for search features or cleaning data.
let fruits = ["apple", "banana", "avocado", "cherry", "apricot"];
// Find all fruits that start with 'a'
let aFruits = fruits.filter(fruit => fruit.startsWith("a"));
console.log(aFruits);
["apple", "avocado", "apricot"]
This method is case-sensitive. If you need case-insensitive search, convert to lowercase first.
Filtering Objects in Arrays
Real data often comes as objects. You can filter arrays of objects by their properties. This is very powerful.
let users = [
{ name: "Alice", age: 25, active: true },
{ name: "Bob", age: 30, active: false },
{ name: "Charlie", age: 22, active: true },
{ name: "Diana", age: 28, active: false }
];
// Get only active users
let activeUsers = users.filter(user => user.active === true);
console.log(activeUsers);
[
{ name: "Alice", age: 25, active: true },
{ name: "Charlie", age: 22, active: true }
]
You can combine conditions too. Find active users over 23 years old.
let activeAdults = users.filter(user => user.active && user.age > 23);
console.log(activeAdults);
[ { name: "Alice", age: 25, active: true } ]
Removing Empty or Falsy Values
Data often has empty strings, null, or undefined values. filter() can clean this up quickly.
let messyData = [0, "hello", "", null, "world", undefined, false, 42];
// Remove all falsy values
let cleanData = messyData.filter(Boolean);
console.log(cleanData);
["hello", "world", 42]
Using filter(Boolean) is a common trick. It removes null, undefined, 0, false, NaN, and empty strings. Only truthy values remain.
Using the Index Parameter
Sometimes you need the position of an item. The index parameter helps with that.
let items = ["a", "b", "c", "d", "e"];
// Get items at even index positions
let evenIndexItems = items.filter((item, index) => index % 2 === 0);
console.log(evenIndexItems);
["a", "c", "e"]
The index starts at 0. So items at positions 0, 2, and 4 are kept.
Chaining Filter with Other Methods
You can chain filter() with other array methods. This makes your code very expressive. For a deeper look at array tools, check out our JavaScript Array Methods Guide.
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Get even numbers, then double them
let result = numbers
.filter(num => num % 2 === 0)
.map(num => num * 2);
console.log(result);
[4, 8, 12, 16, 20]
First, filter() keeps only even numbers. Then map() doubles each one. Chaining keeps your code clean and readable.
Common Mistakes to Avoid
The biggest mistake is forgetting to return a value. If you do not return true or false, the result is an empty array.
let numbers = [1, 2, 3, 4, 5];
// Wrong: no return statement
let wrong = numbers.filter(num => {
num > 2;
});
console.log(wrong);
[]
Always use an explicit return with curly braces. Or use the short arrow syntax without braces.
Another mistake is modifying the original array inside filter. Do not change the array you are filtering. It can cause unexpected behavior.
Performance Tips
filter() is fast for most tasks. But for very large arrays, be careful. The callback runs once for every element. Complex operations inside the callback can slow things down.
If you need to stop early, use a regular for loop instead. Filter always checks every item. It cannot break out early.
Remember that filter() creates a new array. This uses extra memory. For huge datasets, consider other approaches. Understanding how arrays work is key. Read our JavaScript Array Variables Guide for more background.
Real-World Use Cases
Filter is everywhere. You use it to search product lists. You filter user input. You clean API responses. You build to-do lists where you show only completed tasks.
Here is a quick example for a search feature.
let products = [
{ name: "Laptop", price: 1200, category: "electronics" },
{ name: "Shirt", price: 25, category: "clothing" },
{ name: "Phone", price: 800, category: "electronics" },
{ name: "Shoes", price: 60, category: "clothing" }
];
// Search for electronics under $1000
let searchResults = products.filter(product => {
return product.category === "electronics" && product.price < 1000;
});
console.log(searchResults);
[ { name: "Phone", price: 800, category: "electronics" } ]
This is just a taste. You can build complex search logic with filter.
Conclusion
The filter() method is a must-know tool for any JavaScript developer. It helps you extract exactly what you need from an array. It is clean, safe, and easy to read. Practice with numbers, strings, and objects. Chain it with other methods. Avoid common mistakes like missing returns. With these skills, you can handle data like a pro. Start using filter in your projects today. You will wonder how you worked without it.