JavaScript Array filter() Method

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
New array

What You’ll Learn

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

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.

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.

📝 Syntax

General form of Array.prototype.filter:

JavaScript
array.filter(callback(element, index, array), thisArg)

Parameters

  • callback — predicate tested on each element; truthy keeps the item.
  • element — current array item.
  • index — optional; position of the element.
  • array — optional; the array being filtered.
  • thisArg — optional; value to use as this inside the callback.

Return value

  • A new array with all elements that passed the test.
  • [] when no elements pass.
  • Original array unchanged.

Common patterns

  • arr.filter(n => n > 0) — keep positive numbers.
  • users.filter(u => u.active) — keep active records.
  • arr.filter(Boolean) — drop falsy values (0, "", null, etc.).
  • arr.filter(Boolean).map(...) — clean then transform.

⚡ Quick Reference

GoalCode
Filter with testarr.filter(callback)
Keep even numbersarr.filter(n => n % 2 === 0)
Remove falsy valuesarr.filter(Boolean)
Mutates original?No
No matches[]

📋 filter() vs map() vs find() vs every()

All use callbacks, but each answers a different question and returns a different type.

filter
all matches

New array subset

map
transform all

Same length array

find
first match

One element

every
all pass?

Boolean only

Examples Gallery

Open DevTools Console (F12) or use Try-it labs. Example N maps to ?tryit=N.

📚 Getting Started

Filter primitives with simple predicates.

Example 1 — Keep Only Even Numbers

Build a new array containing numbers divisible by 2.

JavaScript
const numbers = [1, 5, 10, 15, 20];

const evenNumbers = numbers.filter((num) => num % 2 === 0);

console.log(evenNumbers);
// [10, 20]
console.log(numbers);
// [1, 5, 10, 15, 20] — unchanged
Try It Yourself

How It Works

Only 10 and 20 pass num % 2 === 0. The source array stays intact.

Example 2 — Filter Values Above a Threshold

Keep numbers strictly greater than 10.

JavaScript
const numbers = [1, 5, 10, 15, 20];

const aboveTen = numbers.filter((num) => num > 10);

console.log(aboveTen);
// [15, 20]
Try It Yourself

How It Works

10 fails because the test is strict (>, not >=). Adjust the operator to include boundary values when needed.

📈 Practical Patterns

Objects, cleanup, and text search.

Example 3 — Filter Objects by a Property

Select students whose grade is above 80.

JavaScript
const students = [
  { name: "Alice", grade: 85 },
  { name: "Bob", grade: 92 },
  { name: "Charlie", grade: 78 }
];

const highPerformers = students.filter((student) => student.grade > 80);

console.log(highPerformers);
// [{ name: "Alice", grade: 85 }, { name: "Bob", grade: 92 }]
Try It Yourself

How It Works

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.

JavaScript
const values = [10, null, 20, undefined, 30, 0];

const cleaned = values.filter((value) => value != null);

console.log(cleaned);
// [10, 20, 30, 0]
Try It Yourself

How It Works

value != null removes both null and undefined but keeps 0 and "". Use filter(Boolean) only when you want to strip all falsy values.

🚀 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 stepsfilter().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.

📝 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.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about Array.filter()

Your foundation for selecting subsets in JavaScript.

5
Core concepts
🗂 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.

Continue to find()

Learn how to return the first element that matches your condition.

find() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful