JS array.filter()

JavaScript has many array methods that can help you solve a variety of problems. One of those methods is the filter() method. The filter() method creates a new array with all of the elements that pass the test implemented by the provided function.


array.filter(function(currentValue, index, array), thisValue)


A function to be run for each element in the array.
function(currentValue, index, array)

Function arguments:


  • The value of the current element
  • Required


  • The array index of the current element
  • Optional


  • The array object the current element belongs to
  • Optional


  • A value to use as this when executing a callback function
  • If this parameter is empty, the value “undefined” will be passed as its this value
  • Optional

Return Value

The return value is a new array containing the elements that passed the test. If no elements passed the test, the return value will be an empty array.


Happy coding!

More Info!

JavaScript docs