Higher-Order Functions (HOF)

Last Updated: 22nd October 2025


A Higher-Order Function (HOF) is a function that either:

  1. Takes another function as an argument,
  2. Or returns a function as its result,
  3. (or does both!)

Hinglish Tip 🗣: HOF woh hota hai jo function ke saath khelta hai — ya to function leta hai, ya function deta hai!

✏️ Basic Example

function greet(name) {
  return `Hello, ${name}`;
}

function displayMessage(fn) {
  console.log(fn("Aarav"));
}

displayMessage(greet);

Example 1:

function multiplier(factor) {
  return function (number) {
    return number * factor;
  };
}

const double = multiplier(2);
console.log(double(5)); // 10

const triple = multiplier(3);
console.log(triple(5)); // 15

Example 2:

function calculate(a, b, operation) {
  return operation(a, b);
}

function add(x, y) {
  return x + y;
}

function multiply(x, y) {
  return x * y;
}

console.log(calculate(5, 3, add)); // 8
console.log(calculate(5, 3, multiply)); // 15

There are many inbuilt higher order functions in JavaScript like map(), filter(), reduce(), etc.We will learn them later.