The array method that creates a new array containing elements that pass a test is filter.
The filter method receives a callback, often called a predicate, and runs it for each element. Whenever the callback returns a truthy value, that element is copied into the result. For example, `[1, 2, 3, 4].filter(n => n % 2 === 0)` produces `[2, 4]`. The original array is not changed.
Several nearby methods have different jobs. find returns the first matching element rather than an array of all matches. map creates a new array with one transformed result for every original element, while reduce combines the elements into a single accumulated value such as a sum or object. Remembering “filter keeps, map changes” is a useful shortcut.
Filter is a classic higher-order function because it accepts another function as an argument. The idea has deep roots in functional programming, where predicates select values from lists and other collections. In JavaScript, Array.prototype.filter was standardized as part of the language’s array methods and is also widely used with objects after converting them with Object.entries or similar utilities.