JavaScript (ES2026) reference · Array Methods

JavaScript Array filter() Method

Return a new array with only the elements that pass a test function. The original array is not changed.

JavaScript (ES2026) Since ES5

Syntax

array.filter(callbackFn, thisArg)

Parameters

ParameterDescription
callbackFn Called for each element with (element, index, array).

Return value

A new array (possibly empty).

Examples

Keep matching elementsTry it yourself
const nums = [5, 12, 8, 130, 44];
console.log(nums.filter((n) => n > 10));

Output

[ 12, 130, 44 ]
Filter objects, then mapTry it yourself
const users = [
  { name: "Ada", active: true },
  { name: "Bob", active: false },
];
console.log(users.filter((u) => u.active).map((u) => u.name));

Output

[ 'Ada' ]
Remove falsy valuesTry it yourself
console.log([0, "", "hi", null, 42].filter(Boolean));

Output

[ 'hi', 42 ]

Version history

VersionChange
JavaScript ES5Added

In other languages