JavaScript treats functions as first-class citizens, allowing us to pass them around, return them, and even compose them. This flexibility gives birth to higher-order functions, a concept that powers much of modern JavaScript β especially array methods like filter()
.
π What is a Higher-Order Function?
A higher-order function is one that either:
- Accepts another function as an argument, or
- Returns a function as its output.
function greetUser(greetingFn) { greetingFn(); } function sayHello() { console.log("Hello, Developer!"); } greetUser(sayHello); // Output: Hello, Developer!
Here, greetUser
is a higher-order function because it takes a function as a parameter.
β Real-World Analogy: Filter Coffee
Imagine youβre brewing coffee through a filter. Only the finest particles pass through, giving you the perfect brew. Similarly, JavaScriptβs filter()
method passes only the elements that meet a condition β defined via a callback function.
π‘ What is filter()
in JavaScript?
The filter() method creates a new array with elements that pass a specific test (provided by a callback).
Syntax:
array.filter(callbackFunction(element, index, array), thisArg)
π§ Why is filter()
a Higher-Order Function?
Because filter()
accepts a callback function, it is by definition a higher-order function.
You define the logic; filter()
applies it dynamically to each array element β making it modular, reusable, and clean.
β
Practical Examples
1. Filter Even Numbers
const numbers = [1, 2, 3, 4, 5]; const even = numbers.filter(num => num % 2 === 0); console.log(even); // [2, 4]
2. Filter Adults from User List
const users = [ { name: "Ankur", age: 28 }, { name: "Rahul", age: 17 }, { name: "Priya", age: 21 } ]; const adults = users.filter(user => user.age >= 18); console.log(adults);
3. Modular Filtering with Named Functions
function isAdult(user) { return user.age >= 18; } const adultUsers = users.filter(isAdult);
π Chaining with Other Array Methods
You can chain filter()
with map()
and reduce()
for powerful data transformations:
const prices = [100, 200, 300, 400]; const discounted = prices .filter(price => price > 200) .map(price => price * 0.9); console.log(discounted); // [270, 360]
β Common Questions
Q: Does filter()
mutate the original array?
A: No. filter()
returns a new array.
Q: Can I filter based on multiple conditions?
A: Yes, just use logical operators in the callback:
const filtered = numbers.filter(num => num > 10 && num < 50);
Q: Can I use filter()
with objects?
A: Use Object.entries()
+ filter()
:
const obj = { a: 1, b: 2, c: 3 }; const filtered = Object.entries(obj) .filter(([key, value]) => value > 1); console.log(Object.fromEntries(filtered)); // { b: 2, c: 3 }
β
Final Thoughts
Understanding filter()
as a higher-order function is essential for writing clean, functional, and maintainable JavaScript. Itβs not just about filtering β itβs about mastering function composition and code reusability.
π Original Blog Link
π https://webcodingwithankur.blogspot.com/2025/07/javascript-filter-higher-order-function.html
Top comments (0)