JavaScript array filter Method | Example code

  • by

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.


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

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.

    <title>Sample Code</title>  
    <script type="text/javascript">  
  	var ages = [12, 28, 16, 50, 10];

	function checkAdult(age) {
  		return age >= 18;


JavaScript array filter Method

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

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

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

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