π§ JavaScript Functions Deep Dive #6: Functional Patterns β First-Class & Higher-Order Functions

JavaScript treats functions as first-class citizens, meaning you can use functions like any other value. This behavior unlocks one of the most powerful programming paradigms: functional programming.
In this article, youβll learn:
β What first-class functions are
β What higher-order functions are
β How they work together in real-world JavaScript
β How they power array methods, callbacks, closures, and more
π₯ First-Class Functions
πΉ Definition
A programming language is said to have first-class functions if functions can be:
Assigned to variables
Passed as arguments
Returned from other functions
Stored in data structures
In JavaScript, functions are values. This means:
// Assign a function to a variable
const greet = function(name) {
return `Hello, ${name}`;
};
// Pass a function as argument
function runCallback(callback) {
return callback("Pushpesh");
}
console.log(runCallback(greet)); // Hello, Pushpesh
π§± First-Class Functions in Action
Action | Example |
β Store in a variable | const log = () => console.log("Hi") |
β Pass to another function | arr.map (callback) |
β Return from a function | function outer() { return function inner() {} } |
β Store in an object or array | const obj = { sayHi: () => {} } |
ποΈ Higher-Order Functions (HOF)
πΉ Definition
A higher-order function is a function that:
Takes one or more functions as arguments, OR
Returns a function as its result
β A higher-order function is powered by first-class functions.
π§ Example 1: Takes a function as argument
function repeat(n, action) {
for (let i = 0; i < n; i++) {
action(i);
}
}
repeat(3, console.log);
// Output: 0 1 2
π§ Example 2: Returns a function
function multiplier(factor) {
return function (n) {
return n * factor;
};
}
const double = multiplier(2);
console.log(double(5)); // 10
π This is function factory behavior β you return new functions based on input.
π§ͺ Higher-Order Functions in the Wild
JavaScript has many built-in higher-order functions:
Method | What it takes | Example |
Array.map | Function | arr.map (fn) |
Array.filter | Function | arr.filter(fn) |
Array.reduce | Function | arr.reduce(fn) |
setTimeout | Function | setTimeout(() => {}, 1000) |
Promise.then | Function | promise.then(fn) |
const nums = [1, 2, 3];
const squared = nums.map(n => n * n);
console.log(squared); // [1, 4, 9]
π― First-Class vs Higher-Order β Whatβs the Difference?
Feature | First-Class Function | Higher-Order Function |
Function is a value? | β Yes | β Yes |
Passed/Returned? | β Yes | β Yes |
Takes/Returns function? | β Not necessarily | β Yes |
Role | Capability | Pattern |
Think of it like this:
First-class is a property of the language
Higher-order is a design pattern made possible by that property
π₯ Real-World Use Case: Custom Logger
function createLogger(level) {
return function (message) {
console.log(`[${level}] ${message}`);
};
}
const errorLog = createLogger("ERROR");
const infoLog = createLogger("INFO");
errorLog("Something went wrong");
infoLog("App started");
β This is a classic HOF β creating new behavior based on configuration.
β Benefits of Using Functional Patterns
Advantage | Description |
β Reusability | You can build reusable utility functions |
β Composition | You can build complex logic by chaining simple parts |
β Declarative code | Array methods like map , filter make code cleaner |
β Separation of concerns | Callback functions isolate behaviors |
π§βπ« Final Thoughts
First-class and higher-order functions are fundamentals of functional JavaScript. They let you:
Write cleaner, more expressive code
Abstract logic into reusable pieces
Embrace callbacks, closures, currying, and more
Theyβre not just theory β they power almost every array method, every async flow, and every functional utility library you use.
Subscribe to my newsletter
Read articles from pushpesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
