🧠 JavaScript Functions Deep Dive #6: Functional Patterns β€” First-Class & Higher-Order Functions

pushpesh kumarpushpesh kumar
4 min read

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

ActionExample
βœ… Store in a variableconst log = () => console.log("Hi")
βœ… Pass to another functionarr.map(callback)
βœ… Return from a functionfunction outer() { return function inner() {} }
βœ… Store in an object or arrayconst 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:

MethodWhat it takesExample
Array.mapFunctionarr.map(fn)
Array.filterFunctionarr.filter(fn)
Array.reduceFunctionarr.reduce(fn)
setTimeoutFunctionsetTimeout(() => {}, 1000)
Promise.thenFunctionpromise.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?

FeatureFirst-Class FunctionHigher-Order Function
Function is a value?βœ… Yesβœ… Yes
Passed/Returned?βœ… Yesβœ… Yes
Takes/Returns function?❌ Not necessarilyβœ… Yes
RoleCapabilityPattern

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

AdvantageDescription
βœ… ReusabilityYou can build reusable utility functions
βœ… CompositionYou can build complex logic by chaining simple parts
βœ… Declarative codeArray methods like map, filter make code cleaner
βœ… Separation of concernsCallback 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.

0
Subscribe to my newsletter

Read articles from pushpesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

pushpesh kumar
pushpesh kumar