JavaScript Array filter() Method

Below is the example of the Array filter() method. 
 

  • Example: 
     

JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // JavaScript to illustrate findIndex() method
    function canVote(age) {
        return age >= 18;
    }
 
    function func() {
        var filtered = [24, 33, 16, 40].filter(canVote);
        document.write(filtered);
    }
    func();
</script>                   

chevron_right


  • Output: 
     
[24,33,40]

The arr.filter() method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method. 
Syntax: 
 

array.filter(callback(element, index, arr), thisValue)

Parameters: This method accepts five parameter as mentioned above and described below: 
 

  • callback: This parameter holds the function to be called for each element of the array.
  • element: The parameter holds the value of the elements being processed currently.
  • index: This parameter is optional, it holds the index of the currentValue element in the array starting from 0.
  • arr: This parameter is optional, it holds the complete array on which Array.every is called.
  • thisValue: This parameter is optional, it holds the context to be passed as this to be used while executing the callback function. If the context is passed, it will be used like this for each invocation of the callback function, otherwise undefined is used as default.

Return value: This method returns a new array consisting of only those elements that satisfied the condition of the arg_function
Below examples illustrate the arr.filter() method in JavaScript:
 

  • Example 1: In this example, the method filter() creates a new array consisting of only those elements that satisfy the condition checked by isPositive() function.
     
function isPositive(value) {
  return value > 0;
}

var filtered = [112, 52, 0, -1, 944].filter(isPositive);
print(filtered);


  • Output: 
     
[112,52,944]


  • Example 2: In this example, the method filter() creates a new array consisting of only those elements that satisfy the condition checked by isPositive() function.
     
function isEven(value) {
  return value % 2 == 0;
}

var filtered = [11, 98, 31, 23, 944].filter(isEven);
print(filtered);


  • Output: 
     
[98,944]


Codes for the above methods are defined as follows: 
Program 1: 
 



JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // JavaScript to illustrate filter() method
    function isPositive(value) {
        return value > 0;
    }
 
    function func() {
        var filtered = [112, 52, 0, -1, 944].filter(isPositive);
        document.write(filtered);
    }
    func();
</script>

chevron_right


Output: 
 

[112,52,944]

Program 2: 
 

JavaScript

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
    // JavaScript to illustrate filter() method
    function isEven(value) {
        return value % 2 == 0;
    }
 
    function func() {
        var filtered = [11, 98, 31, 23, 944].filter(isEven);
        document.write(filtered);
    }
    func();
</script>

chevron_right


Output: 
 

[98,944]

Supported Browsers: The browsers supported by JavaScript Array filter() method are listed below: 
 

  • Google Chrome
  • Microsoft Edge 9.0
  • Mozila Firefox 1.5
  • Safari
  • Opera

 

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.