Favorites

JavaScript Array.prototype.filter() Method

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
Array.prototype.filter()
JavaScript Array Instance methodThis method is supported by all modern browsers.
Updated 28th November 2026

The filter() method creates a new array with only the elements that pass a test you provide. The original array is not changed.

Description

The filter() method creates a new array with every element for which the callback returns a truthy value. The original array is left unchanged, and the elements kept are the same values (objects are not copied).

It's the standard way to remove unwanted items, such as completed tasks or empty strings. To find just the first matching element, use find(), which stops searching as soon as it finds one. filter(Boolean) is a quick way to remove all falsy values.

The callback receives the index as its second argument, which allows filters based on position, and the array as its third, which lets you remove duplicates with arr.filter((v, i, a) => a.indexOf(v) === i), although [...new Set(arr)] is simpler and faster. Empty slots are skipped, so filtering also compacts sparse arrays.

Syntax

array.filter(callbackFn)
array.filter(callbackFn, thisArg)

Parameters

  • callbackFnA function that tests each element. Return a truthy value to keep the element. It receives element, index and array.
  • thisArgOptional. A value to use as this inside callbackFn.

Return Value

A new array containing only the elements that passed the test (a shallow copy). It is empty if none passed.

Example

const numbers = [5, 12, 8, 130, 44];
console.log(numbers.filter(n => n > 10));

const tasks = [{ title: 'Write', done: true }, { title: 'Test', done: false }];
console.log(tasks.filter(t => !t.done).map(t => t.title));

console.log(['a', '', 'b', null].filter(Boolean));

Browser Support

The following information will show you the current browser support for the Array.prototype.filter() method. Hover over a browser icon to see the version that first introduced support for this method.

This method is supported by all modern browsers.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported
☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee