Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

JavaScript Array filter() Method

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The JavaScript Array 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 parameters 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 current 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 is 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

Example 1:  In this example, the method filter() creates a new array consisting of only those elements that satisfy the condition checked by canVote() function.

JavaScript




// JavaScript to illustrate findIndex() method
function canVote(age) {
    return age >= 18;
}
 
function func() {
    let filtered = [24, 33, 16, 40].filter(canVote);
    console.log(filtered);
}
func();


Output:  

[24,33,40]

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.

Javascript




function isPositive(value) {
    return value > 0;
}
 
let filtered = [112, 52, 0, -1, 944].filter(isPositive);
console.log(filtered);


Output: 

[112,52,944]

Example 3: In this example, the method filter() creates a new array consisting of only those elements that satisfy the condition checked by isEven() function. 

Javascript




function isEven(value) {
    return value % 2 == 0;
}
 
let filtered = [11, 98, 31, 23, 944].filter(isEven);
console.log(filtered);


Output: 

[98,944]

We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article.

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

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

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Last Updated : 18 May, 2023
Like Article
Save Article
Similar Reads
Related Tutorials