Javascript Filter | JavaScript Array filter() Method

The javascript 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)

  1. function- This parameter tells the function to be called for each element of the array.
  2. currentValue – This parameter holds the value of the elements being processed currently.
  3. Index – This is optional and it stores the index of the current element in the array starting from 0.
  4. 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.
  5. 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 filter() creates a new array consisting of only those elements that satisfy the condition checked by canBuy() function.

function canBuy(amount){
return amount<=100


Function func(){

Let filtered=[200,32,48,100,56,99].filter(canBuy);



Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *