Array.prototype.filter()

filter(callbackFn, thisArg)
Returns: Array · Updated March 13, 2026 · Array Methods
array filter functional

The filter method 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.

Syntax

filter(callbackFn)
filter(callbackFn, thisArg)

Parameters

ParameterTypeDefaultDescription
callbackFnfunctionrequiredFunction to test each element. Return true to keep the element, false otherwise.
elementany-The current element being processed.
indexnumber-The index of the current element.
arrayArray-The array filter was called upon.
thisArganyundefinedValue to use as this when executing callbackFn.

Examples

Basic usage

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// ['exuberant', 'destruction', 'present']

Filtering with index

const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

// Keep only numbers at even indices
const result = numbers.filter((num, index) => index % 2 === 0);

console.log(result);
// [1, 3, 5, 7, 9]

Filtering objects

const users = [
  { name: 'Alice', age: 25, active: true },
  { name: 'Bob', age: 30, active: false },
  { name: 'Charlie', age: 28, active: true },
  { name: 'Diana', age: 22, active: false }
];

const activeUsers = users.filter(user => user.active);

console.log(activeUsers);
// [{ name: 'Alice', age: 25, active: true },
//  { name: 'Charlie', age: 28, active: true }]

Using thisArg

const nums = [1, 2, 3, 4, 5];

const multiplier = {
  threshold: 3
};

const result = nums.filter(function(num) {
  return num > this.threshold;
}, multiplier);

console.log(result);
// [4, 5]

Common Patterns

Finding unique values

const items = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple'];

const unique = items.filter((item, index) => items.indexOf(item) === index);

console.log(unique);
// ['apple', 'banana', 'orange']

Filtering by multiple criteria

const products = [
  { name: 'Laptop', price: 999, inStock: true },
  { name: 'Mouse', price: 29, inStock: true },
  { name: 'Keyboard', price: 79, inStock: false },
  { name: 'Monitor', price: 299, inStock: true }
];

const affordableInStock = products.filter(p => p.inStock && p.price < 100);

console.log(affordableInStock);
// [{ name: 'Mouse', price: 29, inStock: true }]

Removing falsy values

const mixed = [0, 1, 'hello', '', null, undefined, true, false, 42];

const truthy = mixed.filter(Boolean);

console.log(truthy);
// [1, 'hello', true, 42]

See Also