filter()

ES5+

Creates a new array with all elements that pass the test implemented by the provided function.

Syntax

array.filter(callback(element, index, array), thisArg)

Parameters

callback Function

Function to test each element of the array

Return Value

Array

A new array with elements that pass the test

Examples

JavaScript
const numbers = [1, 2, 3, 4, 5, 6];
const evens = numbers.filter(x => x % 2 === 0);
console.log(evens);
Output:
// [2, 4, 6]

Related Methods