The filter() method builds a new array from elements that pass a test function. This tutorial covers callback syntax, five examples, filtering objects, removing null values, and how filter() differs from map(), find(), and every().
01
Syntax
filter(cb)
02
Predicate
true = keep
03
New array
Subset only
04
Read-only
Source safe
05
No match
Returns []
06
ES5
Universal
Fundamentals
Introduction
Real-world lists are rarely uniform. You might need only even numbers, active users, or products in stock. The filter() method selects matching items and returns them in a fresh array.
It is one of the most-used array methods in modern JavaScript—alongside map() and forEach()—because it expresses “give me the items that fit this rule” in a single readable line.
Concept
Understanding the filter() Method
array.filter(callback) calls your callback once per element. When the callback returns a truthy value, that element is copied into the result array. Falsy returns skip the element.
The original array is never shortened or replaced—you always get a new array reference, which may be empty if nothing passes.
💡
Beginner Tip
Your callback is a predicate: a function that answers yes/no. Return true to keep an element, false to drop it.
Object references from the original array are copied into the result—not deep clones. Mutating a result object also changes it in the source unless you copy explicitly.
Example 4 — Remove null and undefined
Drop missing values while keeping valid numbers like 0.
This is the pattern behind search boxes and autocomplete lists. Chain with map() afterward if you need to highlight or format the results.
Applications
🚀 Common Use Cases
UI lists — show only active, visible, or in-stock items.
Search & sort prep — narrow data before rendering tables.
Validation cleanup — remove null entries from API responses.
Permissions — keep resources the current user may access.
Analytics — isolate events matching criteria for reporting.
Pipeline steps — filter().map() for select-then-transform flows.
🧠 How filter() Runs
1
Create result array
Start with an empty array that will hold matches.
Setup
2
Test each element
Invoke the callback with (element, index, array).
Predicate
3
Collect passes
Truthy callback result → push element to the result array.
Keep
4
🗂
Return new array
Original untouched; result may be shorter or empty.
Important
📝 Notes
Always returns a new array, even when every element passes (shallow copy of references).
Callback should not mutate the array being iterated (avoid splice/shift during filter).
Sparse array holes are skipped unless your engine treats them as undefined visits.
filter(Boolean) removes 0 and ""—often too aggressive for numeric data.
For a single match, find() is clearer and stops at the first hit.
Available since ES5 with excellent browser support.
Compatibility
Browser & Runtime Support
Array.prototype.filter() has been part of JavaScript since ES5 (2009). It is supported virtually everywhere.
✓ Baseline · ES5
Array.prototype.filter()
Supported in Chrome 1+, Firefox 1.5+, Safari 3+, Edge 12+, IE 9+, and all Node.js versions.
99%Universal browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
Array.filter()Universal
Bottom line: One of the safest array methods to use without polyfills. Ideal for production code targeting broad audiences.
Wrap Up
Conclusion
The filter() method is how you express subset selection in modern JavaScript. Write a clear predicate, get a new array of matches, and leave the original data untouched.
Combine it with map() for transform pipelines, or reach for find() when you only need the first match.
Write descriptive predicates: user => user.isActive
Return explicit booleans for readability
Use filter() to select, map() to transform
Chain filter().map() for clean pipelines
Prefer value != null when zero is valid data
❌ Don’t
Mutate the source array inside the callback
Use filter() when you only need one item (find())
Use filter() when you only need yes/no (every() / some())
Assume filter(Boolean) keeps 0 or empty strings
Forget filtered objects share references with the original
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Array.filter()
Your foundation for selecting subsets in JavaScript.
5
Core concepts
📝01
Syntax
arr.filter(cb)
API
🗂02
New array
Subset only.
Output
✅03
Predicate
true = keep.
Logic
🗃04
Source safe
No mutation.
Immutable
🔄05
vs find
All vs first.
Compare
❓ Frequently Asked Questions
filter() creates a new array containing only elements for which the callback returns a truthy value. Elements that fail the test are skipped.
No. filter() always returns a new array. The source array is left unchanged unless your callback deliberately mutates it (which you should avoid).
It returns an empty array [], not null or undefined.
filter() returns all matching elements in a new array. find() returns only the first matching element (or undefined if none match).
filter() only selects elements—it does not change them. Use map() when you need to transform each item. A common pattern is filter().map() chained together.
filter() has been available since ES5 (2009). It works in all modern browsers, Node.js, and Internet Explorer 9+.
Did you know?
filter() always visits every element unless you switch to find() for early exit. If you only need to know whether matches exist, some() can be faster because it stops at the first success.