Filter method in javaScript

Опубликовано: 06 Июнь 2026
на канале: Profu' de geogra'
4
0

Filter method in javaScript
The filter() method in JavaScript is used to create a new array containing all elements from an existing array that pass a specified test. Unlike the find() method, which returns only the first matching element, filter() returns all elements that satisfy the given condition, making it especially useful for filtering out certain items based on a condition. This method does not modify the original array, instead, it returns a new array with the filtered elements.
The filter() method takes a callback function that is executed on each element of the array. This callback function receives three arguments: the current element, its index, and the entire array. However, the most commonly used argument is the current element.
array.filter(callback(element, index, array))
• callback: The function that is applied to each element. It should return true for elements that should be included in the new array.
• element: The current element being processed.
• index (optional): The index of the current element.
• array (optional): The array being processed.
filter() is ideal for situations where you need to select multiple items from an array based on a condition, such as filtering users based on their age, status, or any other attribute.