![]() |
VOOZH | about |
The filter() method creates a new array by selecting elements that meet a specific condition. It does not modify the original array and ignores empty elements.
Syntax
array.filter(callback(element, index, arr), thisValue)Parameters: This method accepts five parameters as mentioned above and described below:
| Parameter | Description |
|---|---|
| callback | The function is to be called for each element of the array. |
| element | The value of the element currently being processed. |
| index | (Optional) The index of the current element in the array, starting from 0. |
| arr | (Optional) The complete array on which filter() is called. |
| thisValue | (Optional) The context to be passed as this to be used while executing the callback function. If not provided, undefined is used as the default context. |
Return value: It returns an array of elements that pass the test and an empty array if no elements pass the test.
Example 1: Creating a new array consisting of only those elements that satisfy the condition checked by isPositive() function.
Example 2: Creating a new array consisting of only those elements that satisfy the condition checked by isEven() function.