Filter is a higher-order function that allows us to distinguish and keep only certain elements of a collection based on a Boolean predicate. Of course, filtering is particularly useful when removing certain elements from a collection based on a certain condition. Take the following code as an example:
function myJS(){ let array = [1, 2, 3]; let arrayEvenNumbers = array.filter(current => current % 2 == 0); // arrayEvenNumbers == [2]}
Filtering is a great way to avoid loops and nested conditions in order to extract some desired dataset.