Array

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

JavaScript
array.filter(callbackFn, thisArg?)

Parameters

ParameterTypeDescription
callbackFn(element, index, array) => booleanFunction to test each element
thisArganyValue 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

Basic Usage
const numbers = [1, 2, 3, 4, 5, 6];
const even = numbers.filter(n => n % 2 === 0);
console.log(even); // [2, 4, 6]
Practical Example
const words = ['hello', 'hi', 'hey', 'world'];
const short = words.filter(w => w.length <= 3);
console.log(short); // ['hi', 'hey']
Advanced Usage
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); // 2

Understanding 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

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.