Learning Higher-Order Functions in JavaScript

What is a Higher-Order Function?

A higher-order function is a function that can do one or both of these things:

In JavaScript, functions are like any other value (like numbers or text). You can pass them around, store them in variables, or return them from other functions. This makes higher-order functions possible.

Why Are They Useful?

Simple Example

Here's an example where one function uses another function as an input.

            
    function greet(callback) {
        console.log("Executing the greet function...");
        callback();
    }
    
    function sayHello() {
        console.log("Hello, I am the callback function!");
    }
    
    greet(sayHello);
            
        

Output:

Click the button below to see the output.

Next-Level Example: Making a Function

A higher-order function can also create and give back a new function. This is handy for making reusable tools, like a function that multiplies numbers.

            
    function multiplier(factor) {
        return function(number) {
            return number * factor;
        };
    }
    
    const double = multiplier(2);
    const triple = multiplier(3);
    
    console.log(double(5)); // 10
    console.log(triple(5)); // 15
            
        

Output:

Click the button below to see the output.

Real-Life Example: Working with Arrays

JavaScript has built-in tools like map and filter that are higher-order functions because they use other functions to do their job.

            
    const numbers = [1, 2, 3, 4, 5];
    
    // Using map to double each number
    const doubled = numbers.map(num => num * 2);
    console.log("Doubled numbers:", doubled);
    
    // Using filter to get even numbers
    const evens = numbers.filter(num => num % 2 === 0);
    console.log("Even numbers:", evens);
            
        

Output:

Click the button below to see the output.

Key Things to Know

  1. Functions Are Flexible: In JavaScript, you can treat functions like numbers or words—store them, pass them, or return them.
  2. Callbacks: These are functions you give to another function to run later, like when a button is clicked.
  3. Memory Trick: When a function returns another function, the new function can “remember” values from the original function. This is called a closure.
  4. Save Time: Higher-order functions let you write less code by reusing logic.
  5. Handle Delays: They’re often used for things that take time, like waiting for a timer or a web request.

Why They’re Great

Things to Watch Out For

Try It Yourself: Custom Filter

Let’s make a higher-order function that picks items from an array based on a rule you give it.

            
    function customFilter(array, condition) {
        const result = [];
        for (let item of array) {
            if (condition(item)) {
                result.push(item);
            }
        }
        return result;
    }
    
    const numbers = [1, 2, 3, 4, 5, 6];
    const isEven = num => num % 2 === 0;
    const greaterThanThree = num => num > 3;
    
    console.log("Even numbers:", customFilter(numbers, isEven));
    console.log("Numbers > 3:", customFilter(numbers, greaterThanThree));
            
        

Output:

Click the button below to see the output.