JavaScript array filter Method | Example code

JavaScript array filter method is to create a new array and filled with all array elements which passed a condition or test. It will return a new array with the elements that pass the condition. If no elements pass the test, an empty array will be returned.

Note: Filter method is not changed a given array.

Syntax

Parameter Values

  • function: To test each element of the array.
    • currentValue: current element value
    • index: Optional index of current element.
    • arr: Optional array object.
  • thisValue: use as this when executing callback.

Example of JavaScript array filter Method

Let’s try an example where get all the values in the ages an array that is 18 or over.

Output:

JavaScript array filter Method

Do comment if you have any doubts and suggestion on this tutorial.

Note: All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.