Table of contents
1.
Introduction
2.
Syntax of JavaScript Array Filter()
3.
Parameters of JavaScript Array Filter()
4.
Return Value of Array Filter() in JavaScript
5.
How JavaScript Array Filter() Works?
6.
Examples
6.1.
Example 1: Filtering Even Numbers using Array Filter In JavaScript
6.2.
Javascript
6.3.
Example 2: Filtering Words with More Than 4 Letters using JavaScript Array Filter() Method
6.4.
Javascript
6.5.
Example 3: Filtering Objects Based on a Property using JavaScript Array Filter() Method
6.6.
JavaScript
7.
How to Filter an Array with the filter() Method?
8.
How to Filter an Object in JavaScript?
9.
Limitations of JavaScript Array Filter()
10.
Supported Browsers for JavaScript Array Filter()
11.
Frequently Asked Questions
11.1.
What does array filter do in JavaScript?
11.2.
What are the arguments for filter in JavaScript?
11.3.
What is the purpose of array in JavaScript?
12.
Conclusion
Last Updated: Jan 16, 2025
Easy

JavaScript Array filter() Method

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The filter() method in JavaScript creates a new array containing elements that pass a test implemented by a provided function. It iterates through each element, passing it to the callback function - if the function returns true, the element is included in the resulting array. The original array remains unchanged.

JavaScript Array filter() Method

In this article, we will learn about the JavaScript Array filter() Method, its syntax, parameters, return value, and how the filter() method works. 

Syntax of JavaScript Array Filter()

The syntax of the filter() method is simple:

array.filter(callback(element, index, array), thisArg);

Parameters of JavaScript Array Filter()

  • Callback function: A function that is executed for each array element. Return true to keep the element, and false otherwise. This function is provided three arguments:

→ element: The current element being processed.

→ index (optional): The index of the current element being processed in the array.

→ array (optional): The original array being processed. 

  • thisArg (optional): This is the value to use as this when executing the callback function.

Return Value of Array Filter() in JavaScript

The filter() method returns a new array with all the elements that pass the test implemented by the provided callback function. If no elements pass the test, it returns an empty array.

How JavaScript Array Filter() Works?

The JavaScript filter() method works by executing a callback function on each element of an array. This function should return either true or false. When true, the current element is included in the new array; when false, it's excluded. The callback can access three parameters: the current element, its index, and the original array.

array.filter(function(element, index, array) {
   return condition;
});

Examples

Let's look at a few examples to understand how the filter() method works in real scenarios.

Example 1: Filtering Even Numbers using Array Filter In JavaScript

We want to filter all the even numbers from an array:

  • Javascript

Javascript

const numbers = [1, 2, 3, 4, 5, 6];

const evenNumbers = numbers.filter(function(num) {

   return num % 2 === 0;

});

console.log(evenNumbers);
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

[2, 4, 6]

 

In this example, the callback function checks if a number is divisible by 2. If the condition returns true, the number is included in the new array.

Example 2: Filtering Words with More Than 4 Letters using JavaScript Array Filter() Method

We will filter words that have more than 4 letters:

  • Javascript

Javascript

const words = ['apple', 'banana', 'cat', 'dog', 'elephant'];



const longWords = words.filter(function(word) {

   return word.length > 4;

});


console.log(longWords);
You can also try this code with Online Javascript Compiler
Run Code

 

Output:

['apple', 'banana', 'elephant']


In this example, the filter checks the length of each word. Words with more than 4 letters are returned in the new array.

Example 3: Filtering Objects Based on a Property using JavaScript Array Filter() Method

We can also filter arrays of objects. Here’s an example where we filter students who scored above 50 marks:

  • JavaScript

JavaScript

const students = [

   {name: 'John', score: 45},

   {name: 'Jane', score: 75},

   {name: 'Jim', score: 60},

];

const passingStudents = students.filter(function(student) {

   return student.score > 50;

});

console.log(passingStudents);
You can also try this code with Online Javascript Compiler
Run Code


Output:

[ { name: 'Jane', score: 75 }, { name: 'Jim', score: 60 } ]

 

In this example, the callback function tests if the score property of each student object is greater than 50.

How to Filter an Array with the filter() Method?

The filter() method provides a clean way to filter array elements. You pass a callback function that defines the filtering condition, and it returns a new array with matching elements. The callback function can take three parameters: the current element, index, and array. Here's how to use it:

const ages = [16, 25, 18, 30, 14, 28];
const adults = ages.filter(age => age >= 18);
// Result: [25, 18, 30, 28]

// With multiple conditions
const numbers = [1, 2, 3, 4, 5, 6];
const evenOver3 = numbers.filter(num => num > 3 && num % 2 === 0);

How to Filter an Object in JavaScript?

To filter objects in JavaScript, you can convert the object to an array using Object.entries(), Object.keys(), or Object.values(), then apply the filter() method. After filtering, you can convert back to an object if needed. Here's the approach:

const person = {
    name: "Rahul",
    age: 30,
    salary: 50000,
    city: "India"
};

const filtered = Object.entries(person)
    .filter(([key, value]) => typeof value === "number")
    .reduce((obj, [key, value]) => {
        obj[key] = value;
        return obj;
    }, {});

Limitations of JavaScript Array Filter()

  • Cannot modify the original array - always creates a new array
  • Memory intensive for large arrays as it creates a copy
  • Cannot transform elements during filtering (unlike map())
  • Only works with arrays, not with objects directly
  • Cannot stop/break the iteration once a condition is met
  • Doesn't provide a way to access the previously filtered elements
  • Performance can be impacted when filtering large datasets
  • No built-in functionality for deep filtering of nested arrays
  • Cannot handle asynchronous filtering operations natively
  • May return empty array if no elements match the condition

Supported Browsers for JavaScript Array Filter()

The filter() method is supported by all modern browsers, including:

BrowserSupported Versions
Google Chrome45 and later
Mozilla Firefox25 and later
Microsoft Edge12 and later
Safari9 and later
Opera32 and later

For older browsers like Internet Explorer 8 and below, filter() is not supported. In such cases, you can use polyfills to ensure compatibility.

Frequently Asked Questions

What does array filter do in JavaScript?

The filter() method creates a new array containing elements that satisfy a provided condition, leaving the original array unchanged.

What are the arguments for filter in JavaScript?

The filter() method accepts three arguments: the current element, its index, and the array being processed, with the callback returning a boolean.

What is the purpose of array in JavaScript?

An array in JavaScript stores multiple values in a single variable, allowing indexed access, manipulation, and iteration for efficient data handling.

Conclusion

The JavaScript Array filter() Method is a powerful and commonly used tool for working with arrays. It allows developers to extract elements that meet specific conditions, making code cleaner and more efficient. Whether you're filtering numbers, strings, or objects, the filter() method simplifies tasks by returning a new array with the desired elements.

Live masterclass