Get to know MDN better
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The filter() method of Array instances creates a shallow copy of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.
A function to execute for each element in the array. It should return a truthy value to keep the element in the resulting array, and a falsy value otherwise. The function is called with the following arguments:
elementThe current element being processed in the array.
indexThe index of the current element being processed in the array.
arrayThe array filter() was called upon.
thisArg OptionalA value to use as this when executing callbackFn. See iterative methods.
A shallow copy of the given array containing just the elements that pass the test. If no elements pass the test, an empty array is returned.
The filter() method is an iterative method. It calls a provided callbackFn function once for each element in an array, and constructs a new array of all the values for which callbackFn returns a truthy value. Array elements which do not pass the callbackFn test are not included in the new array. Read the iterative methods section for more information about how these methods work in general.
callbackFn is invoked only for array indexes which have assigned values. It is not invoked for empty slots in sparse arrays.
The filter() method is generic. It only expects the this value to have a length property and integer-keyed properties.
The following example uses filter() to create a filtered array that has all elements with values less than 10 removed.
The following example returns all prime numbers in the array:
Note: The isPrime() implementation is for demonstration only. For a real-world application, you would want to use a heavily memoized algorithm such as the Sieve of Eratosthenes to avoid repeated calculations.
The following example uses filter() to create a filtered array of all objects with non-zero, numeric id.
The following example uses filter() to filter array content based on search criteria.
The array argument is useful if you want to access another element in the array, especially when you don't have an existing variable that refers to the array. The following example first uses map() to extract the numerical ID from each name and then uses filter() to select the ones that are greater than its neighbors.
The array argument is not the array that is being built — there is no way to access the array being built from the callback function.
filter() will skip empty slots.
The filter() method reads the length property of this and then accesses each property whose key is a nonnegative integer less than length.
| ECMAScript® 2027 Language Specification # sec-array.prototype.filter |
Enable JavaScript to view this browser compatibility table.
This page was last modified on Dec 13, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.