Array.prototype.filter
Creates a shallow copy of a portion of a given array, filtered down to just the elements that pass the test implemented by the provided function
Syntax
array.filter(callbackFn, thisArg?)Parameters
| Parameter | Type | Description |
|---|---|---|
| callbackFn | (element, index, array) => boolean | Function to test each element |
| thisArg | any | Value to use as this when executing callbackFn |
Return Value
A shallow copy of the given array containing just the elements that pass the test
Examples
const numbers = [1, 2, 3, 4, 5, 6];
const even = numbers.filter(n => n % 2 === 0);
console.log(even); // [2, 4, 6]const words = ['hello', 'hi', 'hey', 'world'];
const short = words.filter(w => w.length <= 3);
console.log(short); // ['hi', 'hey']const products = [
{ name: 'Phone', price: 699 },
{ name: 'Cable', price: 10 },
{ name: 'Laptop', price: 999 }
];
const expensive = products.filter(p => p.price > 100);
console.log(expensive.length); // 2Understanding Array.prototype.filter
The Array.prototype.filter method in JavaScript creates a shallow copy of a portion of a given array, filtered down to just the elements that pass the test implemented by the provided function. It belongs to the Array object and is one of the most widely used methods for working with array values in modern JavaScript and TypeScript applications.
The method signature is array.filter(callbackFn, thisArg?). It accepts 2 parameters: callbackFn, thisArg. When called, it returns a shallow copy of the given array containing just the elements that pass the test. Understanding when and how to use filter() helps you write more expressive, readable code.
Common use cases for Array.prototype.filter include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like array-map, array-find, array-some, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Array.prototype.filter is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Array.prototype.mapCreates a new array populated with the results of calling a provided function on every element in the calling array
Array.prototype.findReturns the first element in the provided array that satisfies the provided testing function
Array.prototype.someTests whether at least one element in the array passes the test implemented by the provided function
Array.prototype.everyTests whether all elements in the array pass the test implemented by the provided function
More Array Methods
Other methods in the Array object
Related Tools
More Array Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.