The javascript array filter() creates a new array with all elements that pass the test implemented by the provided function. It does not disturb the original array. The filter() method doesn’t execute for empty elements.
SYNTAX – array.filter(function(currentValue, index, arr), thisValue)
- function- This parameter tells the function to be called for each element of the array.
- currentValue – This parameter holds the value of the elements being processed currently.
- Index – This is optional and it stores the index of the current element in the array starting from 0.
- arr- This parameter is optional and it holds the complete array on which Array.every (tests whether all the elements pass the provided function) is called.
- thisValue – This is optional, it holds the context to be passed as this is to be used while executing the callback function. If it is passed, it has to be used like this for every invocation of the callback function, or else undefined is used as default.
For Example – In this example, the method of javascript array filter() creates a new array consisting of only those elements that satisfy the condition checked by canBuy() function.