Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

JavaScript Array filter() Method

Posted on December 31, 2024December 31, 2024 By Admin No Comments on JavaScript Array filter() Method

The Array.filter() method is a powerful and widely used JavaScript function that creates a new array containing all the elements from the original array that satisfy a given condition. It does not modify the original array but returns a new one with the filtered elements.

Syntax

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

Parameters

ParameterDescription
callback(Required) A function that tests each element of the array. Elements that pass the test are included in the new array.
element(Required) The current element being processed in the array.
index(Optional) The index of the current element being processed.
array(Optional) The array filter() was called on.
thisArg(Optional) A value to use as this when executing the callback function. Defaults to undefined.

Return Value

The filter() method returns a new array containing all elements that satisfy the condition implemented by the callback function. If no elements satisfy the condition, it returns an empty array.

Example 1: Filtering Numbers

let arr = [1, 2, 3, 4, 5, 6];
let evenNums = arr.filter(num => num % 2 === 0);
console.log(evenNums); 
// Output: [2, 4, 6]

Example 2: Filtering Strings

The filter() method selects strings with more than 5 characters.

let arr = ['apple', 'banana', 'cherry', 'date'];
let filteredArr = arr.filter(item => item.length > 5);
console.log(filteredArr); 
// Output: ['banana', 'cherry']

Example 3: Filtering Objects in an Array

This example filters out users who are not active.

let users = [
    { id: 1, name: 'John', isActive: true },
    { id: 2, name: 'Alice', isActive: false },
    { id: 3, name: 'Bob', isActive: true }
];

let activeUsers = users.filter(user => user.isActive);
console.log(activeUsers); 
/* Output: [
    { id: 1, name: 'John', isActive: true }, 
    { id: 3, name: 'Bob', isActive: true }
]

Example 4: Filtering with Index

The filter() method includes elements at even indices in the new array.

let numbers = [10, 20, 30, 40, 50];

let result = numbers.filter((num, index) => index % 2 === 0);
console.log(result); // Output: [10, 30, 50]

Example 5: Filtering with thisArg

The thisArg parameter allows you to bind a custom context to the callback function.

let context = { threshold: 15 };

let numbers = [10, 20, 30, 40];

let greaterThanThreshold = numbers.filter(function(num) {
  return num > this.threshold;
}, context);

console.log(greaterThanThreshold); 
// Output: [20, 30, 40]

Example 6: Filtering an Empty Array

When called on an empty array, filter() returns an empty array.

let emptyArray = [];

let result = emptyArray.filter(num => num > 0);
console.log(result); // Output: []

Supported Browsers

BrowserSupport
Chrome1+
Firefox1.5+
Safari3+
Edge12+
Opera10.5+
Internet Explorer9+

Note:

  • Large Arrays: For very large arrays, the execution time of the callback function can impact performance.
  • Chaining: Works well when chained with other methods like map() and reduce() for complex data transformations.
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array with() Method
Next Post: JavaScript Array toLocaleString() Method

Leave a Reply Cancel reply

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

  • JavaScript Array Methods
  • JavaScript Array.from() Method
  • JavaScript Array isArray() Method
  • JavaScript Array at() Method
  • JavaScript Array every() Method
  • JavaScript Array filter() Method
  • JavaScript Array find() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array indexOf() Method
  • JavaScript Array join() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method
  • JavaScript Array map() Method
  • JavaScript Array pop() Method
  • JavaScript Array push() Method
  • JavaScript Array reduce() Method
  • JavaScript Array reduceRight() Method
  • JavaScript Array reverse() Method
  • JavaScript Array shift() Method
  • JavaScript Array slice() Method
  • JavaScript Array some() Method
  • JavaScript Array sort() Method
  • JavaScript Array splice() Method
  • JavaScript Array toLocaleString() Method
  • JavaScript Array toReversed() Method
  • JavaScript Array toSorted() Method
  • JavaScript Array toSpliced() Method
  • JavaScript Array toString() Method
  • JavaScript Array unshift() Method
  • JavaScript Array values() Method
  • JavaScript Array with() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme