๐งฉ Tailwind + React + Vite: A Full Setup and Starter Template

JavaScript is a language designed to treat functions as first-class citizens. This means you can pass functions as arguments, return them from other functions, and assign them to variables — opening the door to higher-order functions. One of the most widely used higher-order functions is filter()
.
In this blog, we'll take a deep dive into:
filter()
and why it's considered a higher-order function?filter()
A higher-order function is any function that does at least one of the following:
function greetUser(greetingFn) {
greetingFn();
}
function sayHello() {
console.log("Hello, Developer!");
}
greetUser(sayHello); // Output: Hello, Developer!
Imagine you're in a cafรฉ, and you want your coffee filtered — only what meets the criteria (taste, aroma, strength) passes through. Similarly, the JavaScript filter()
method processes elements in an array and filters out the ones that match a given condition.
filter()
in JavaScript?
The filter()
method is an array method that creates a new array filled with elements that pass a test (provided by a callback function).
array.filter(callbackFunction(element, index, array), thisArg)
filter()
a Higher-Order Function?
Because it accepts a callback function as its argument, filter()
is a textbook example of a higher-order function.
filter()
in JavaScript
const numbers = [1, 2, 3, 4, 5, 6];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4, 6]
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);
function isAbove18(user) {
return user.age > 18;
}
const adultUsers = users.filter(isAbove18);
filter()
?Feature | Why It Matters |
---|---|
Dynamic behavior | Change filter logic on the fly |
Modularity | Keep filtering logic separate |
Reusability | Use the same filter function across components |
Immutability | filter() returns a new array without modifying the original |
Clean Syntax | Easy to read and expressive |
const prices = [100, 200, 300, 400];
// Filter and then apply 10% discount
const discountedPrices = prices
.filter(price => price > 200)
.map(price => price * 0.9);
console.log(discountedPrices); // [270, 360]
Concept | Definition | Example |
---|---|---|
Currying | Breaking a function into smaller functions | const add = a => b => a + b; |
Memoization | Caching function results | Useful for expensive filters |
Callback Function | Function passed to another | Used in filter() |
Function Composition | Combining functions to build complex logic | filter().map().reduce() |
filter()
on non-array data?No, but you can convert array-like structures using Array.from()
.
filter()
modify the original array?No. It returns a new array.
filter()
better than for
loops?For readability and clean code, yes. But traditional loops might perform better in edge cases.
const filtered = numbers.filter(num => num > 10 && num < 50);
filter()
be used on objects?
const obj = { a: 1, b: 2, c: 3 };
const filteredEntries = Object.entries(obj)
.filter(([key, value]) => value > 1);
console.log(Object.fromEntries(filteredEntries)); // { b: 2, c: 3 }
Using filter()
as a higher-order function is more than just filtering values — it’s about writing elegant, modular, and reusable code.
Check out my in-depth post on useMemo in React for performance optimization.
Enjoyed this post? Share it and follow the blog for more JavaScript tips!
Comments
Post a Comment