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
| Parameter | Description |
|---|---|
callbackFn |
Called for each element with (element, index, array). |
Return value
A new array (possibly empty).
Examples
const nums = [5, 12, 8, 130, 44];
console.log(nums.filter((n) => n > 10));
Output
[ 12, 130, 44 ]
const users = [
{ name: "Ada", active: true },
{ name: "Bob", active: false },
];
console.log(users.filter((u) => u.active).map((u) => u.name));
Output
[ 'Ada' ]
console.log([0, "", "hi", null, 42].filter(Boolean));
Output
[ 'hi', 42 ]
Version history
| Version | Change |
|---|---|
| JavaScript ES5 | Added |
In other languages
filter()· Python
Related
array.map()· Return a new array with the results of calling a function on every element. The original array is unchanged.array.find()· Return the first element that passes a test function, or undefined if none does.array.some()· Return true if at least one element passes a test function. Stops at the first match; an empty array returns false.array.reduce()· Reduce the array to a single value by running a reducer function on each element, carrying an accumulator along.- JavaScript Arrays · tutorial
- JavaScript Array Methods · tutorial