JavaScript Array Filter Method

The Array Filter method in JavaScript takes each element in an array and it applies a conditional statement against it.

Reading Time: < 1 minute

If this condition returns true, the element gets pushed to the output array. If the condition returns false, the element does not get pushed to the output array.

The filter() method creates a new array with elements that fall under a given criteria from an existing array.

Also, Learn about Array Map Method in JavaScript

Syntax

The syntax of Array filter() is similar to Array Map, except the callback function should return true to keep the element, or false otherwise. In the callback, only the callback is required.

Examples

The first example is pretty simple and straight forward, where the odd numbers are filtered out, and leaving only even numbers.

In another example, array filter can be used to get all the students whose grades are more than or equal to 90%.

Comments