A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript-higher-order-functions/ below:

JavaScript Higher Order Functions - GeeksforGeeks

JavaScript Higher Order Functions

Last Updated : 23 Jul, 2025

A higher-order function is a function that does one of the following:

Higher-order functions help make your code more reusable and modular by allowing you to work with functions like any other value.

JavaScript
function fun() {
    console.log("Hello, World!");
}
function fun2(action) {
    action();
    action();
}

fun2(fun);

In this example

Popular Higher Order Functions in JavaScript 1. map

The map function is used to transform an array by applying a callback function to each element. It returns a new array.

JavaScript
const n = [1, 2, 3, 4, 5];
const square = n.map((num) => num * num);
console.log(square);
2. filter

The filter function is used to create a new array containing elements that satisfy a given condition.

JavaScript
const n = [1, 2, 3, 4, 5];
const even = n.filter((num) => num % 2 === 0);
console.log(even);
3. reduce

The reduce function accumulates array elements into a single value based on a callback function.

JavaScript
const n = [1, 2, 3, 4, 5];
const sum = n.reduce((acc, curr) => acc + curr, 0);
console.log(sum); 
4. forEach

The forEach function executes a provided function once for each array element.

JavaScript
const n = [1, 2, 3];
n.forEach((num) => console.log(num * 2));
5. find

The find function returns the first element in the array that satisfies a given condition.

JavaScript
const n = [1, 2, 3, 4, 5];
const fEven = n.find((num) => num % 2 === 0);
console.log(fEven);
6. some

The some function checks if at least one array element satisfies a condition.

JavaScript
const n = [1, 2, 3, 4, 5];
const hasNeg = n.some((num) => num < 0);
console.log(hasNeg);
7. every

The every function checks if all array elements satisfy a condition.

JavaScript
const n = [1, 2, 3, 4, 5];
const allPos = n.every((num) => num > 0);
console.log(allPos)
Advanced Techniques with Higher Order Functions 1. Function Composition

Function composition is the process of combining multiple functions to create a new function. The composed function applies multiple operations in sequence.

JavaScript
function add(x) {
    return x + 2;
}
function mul(x) {
    return x * 3;
}

function compose(f, g) {
    return function(x) {
        return f(g(x));
  };
}
var res = compose(add, mul)(4);
console.log(res);
2. Currying

Currying transforms a function that takes multiple arguments into a series of functions that each take one argument. This allows partial application of the function.

JavaScript
function mul(x) {
    return function(y) {
        return x * y;
  };
}
var mulFn = mul(2);
console.log(mulFn(5));
3. Memoization

Memoization is a technique where function results are cached so that repeated calls with the same arguments return faster. This is particularly useful for expensive function calls.

JavaScript
function memoize(func) {
    var cache = {};
    return function (arg) {
        if (cache[arg]) {
            return cache[arg];
        } else {
            var res = func(arg);
            cache[arg] = res;
            return res;
        }
    };
}
function slow(num) {
    console.log("Computing...");
    return num * 2;
}

var fast = memoize(slow);
console.log(fast(5)); // Computing... 10
console.log(fast(5)); // 10 (cached)
Use case's of higher order functions 1. Passing Functions as Arguments

In the following example, we define a Higher-Order Function called greet that accepts a callback function as an argument and executes it

JavaScript
function greet(name, callback) {
    console.log("Hello, " + name);
    callback();
}

function sayGoodbye() {
    console.log("Goodbye!");
}


//Driver Code Starts
greet("Ajay", sayGoodbye);

//Driver Code Ends
2. Returning Functions from Functions

Higher-order functions can also return a function. This enables the creation of more dynamic behavior

JavaScript
function mul(factor) {
    return function(num) {
        return num * factor;
    };
}


//Driver Code Starts
const mul2 = mul(2);
console.log(mul2(5));
const mul3 = mul(3);
console.log(mul3(5)); 
//Driver Code Ends
3. Array Method map() as a Higher-Order Function

JavaScript array methods such as map(), filter(), and reduce() are excellent examples of higher-order functions. These methods take callback functions as arguments and provide powerful ways to manipulate arrays.

JavaScript
const a = [1, 2, 3, 4, 5];
const double = a.map(function(n) {
    return n * 2;
});

console.log(double); 
4. Array Method filter() as a Higher-Order Function

The filter() method is another array function that is a higher-order function. It filters the elements of an array based on a condition provided by the callback function.

JavaScript
const a = [1, 2, 3, 4, 5];
const even = a.filter(function(n) {
    return n % 2 === 0;
});

console.log(even);
5. Array Method reduce() as a Higher-Order Function

The reduce() method is a powerful higher-order function used to reduce an array to a single value.

JavaScript
const n = [1, 2, 3, 4, 5];
const sum = n.reduce(function(acc, curr) {
    return acc + curr;
}, 0);
console.log(sum);


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4