JavaScript – Filter Array

JavaScript Array filter() method is used to filter the elements of this Array using the callback function. Array.filter() method returns a new array with elements of the original array that returned true for the callback function.

Syntax

The syntax to call filter() method on an array x with a callback function callback is

ADVERTISEMENT

Examples

In the following example, we take an array of numbers, and filter only even numbers using Array.filter() method.

index.html

In the following example, we filter strings that start with character b using Array.filter() method.

index.html

Conclusion

In this JavaScript Tutorial, we have learnt how to use filter() method on an Array to filter the elements of an Array based on a function.