The Wayback Machine - https://web.archive.org/web/20240529194623/https://www.geeksforgeeks.org/javascript-higher-order-functions/
Open In App

JavaScript Higher Order Functions

Last Updated : 03 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, a higher-order function is a function that can accept other functions as arguments, return functions, or both. They enable abstraction, composition, and the creation of more flexible and reusable code.

Syntax:

function higherOrderFunction(callback) {
// Perform some operations
// Call the callback function
callback();
}
function callbackFunction() {
console.log("Callback function is executed.");
}
// Passing the callback function to the higher-order function
higherOrderFunction(callbackFunction);

Parameters:

  • higherOrderFunction: Takes a callback function, executes it, and performs operations.
  • callback: A function passed as an argument, executed inside higherOrderFunction.
  • callbackFunction(): Logs “Callback function is executed.”
  • Invocation: Calls higherOrderFunction(callbackFunction), executing callbackFunction within higherOrderFunction.

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Function as an Argument:

This approach involves passing a function (callback) as an argument to another function. The receiving function can then execute the callback, enabling flexible and customizable behavior in JavaScript programs.

Example: In this example, Two functions: greet accepts name and returns a greeting. greet_name combines greeting, message, and name, logging a customized message. Output: “Hi!! JavaScript Welcome To GeeksForGeeks.

Javascript




function greet(name) {
    return `Hi!! ${name} `;
}
  
function greet_name(greeting, message, name) {
    console.log(`${greeting(name)} ${message}`);
}
  
greet_name(greet, 'Welcome To GeeksForGeeks', 'Geeks');


Output

Hi!! Geeks  Welcome To GeeksForGeeks

Approach 2 : Functions as Return Values:

Higher-order functions can also return new functions. This is often used for creating specialized functions or closures. For instance, you can create a function factory that generates functions with specific behavior.

Example: In this example, `multiplier` is a higher-order function that takes a `factor` as an argument and returns a new function that multiplies any number by that factor. We then use `double` and `triple` to create specialized functions.

Javascript




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


Output

10
15


Similar Reads

Higher-Order Arrow Functions in JavaScript
Prerequisite: Arrow Functions A Higher-Order function is a function that receives a function as an argument otherwise returns the function as output. The higher-Order Arrow function implies using arrow functions (in ES6) along with Higher-Order functions. Needs of Higher Order Arrow Function: In a general way, the programmer instructs on how to per
6 min read
Difference between First-Class and Higher-Order Functions in JavaScript
Understanding the difference between first-class and higher-order functions in JavaScript is really important. These are big concepts in programming, especially in the kind of coding used for making websites. This article is all about explaining what they are, how they are used, and why they are so important. 1. First-Class Function A programming l
3 min read
Higher Order Functions and Currying
Introduction: Generally while programming, we use first class functions which means that the programming language treats functions as values – that you can assign a function into a variable, pass it around. These functions do not take other functions as parameters and never has any function as their return type.To overcome all these shortcomings of
4 min read
What is Higher Order Function in JavaScript ?
A higher-order function is a function that can accept a function as one of its parameters and it can also return a function as its return value or it can do both return as well as accept a function. A higher-order function can also take as well as return other types of values but it either has to take a function as a parameter or return a function
1 min read
React.js Higher-Order Components
Higher-order components or HOC is the advanced method of reusing the component functionality logic. It simply takes the original component and returns the enhanced component. Syntax: const EnhancedComponent = higherOrderComponent(OriginalComponent); Reason to use Higher-Order component: Easy to handleGet rid of copying the same logic in every compo
2 min read
Common use cases for Higher-Order Components in React
A Higher-Order Component (HOC) is a handy tool in React. It's like a wrapper for components that can add extra features or modify how they work. So, you use HOCs to excite up your components, making them more versatile without messing up their original code. It's like giving your components a little upgrade! Use cases for Higher-Order Components:Co
2 min read
Advantages of using custom hooks over higher-order components (HOCs) or render props?
Custom hooks, higher-order components (HOCs), and render props are all techniques used in React to share and reuse logic across components. Each approach has its own advantages. Custom hooks in React are JavaScript functions that utilize built-in React hooks or other custom hooks to encapsulate reusable logic in functional components. Higher-order
2 min read
How to use Higher-Order Components in React ?
Higher-Order Component is an advanced function, which reuses component logic in a React Component and returns an enhanced new component. It wraps commonly used processes to allow their use by different components, such as state modification or props change. Components are wrapped in HOCs, whose goal is to improve them without changing their origina
5 min read
Why would you use a Higher-Order Component in React ?
A Higher-Order Component (HOC) in React is used to share common functionality between multiple components. It's like a function that takes a component as an argument and returns a new enhanced component with added features. By offering a pattern for reusing component logic, HOCs help in crafting more maintainable and cleaner React applications. Thi
3 min read
Does JavaScript have higher precedence than CSS ?
While working on the front-end part of your project, CSS plays a major role in defining the styles for your web page. With the help of knowledge in DOM (Document Object Model) and JavaScript, it is possible to change styles for your web page using script files. In this article, we will discuss the precedence of JavaScript with respect to CSS in ter
2 min read