DEV Community

Ramesh Vishnoi
Ramesh Vishnoi

Posted on • Updated on

High Order Function Explained

One of the commonly asked question for Frontend Interview. This question is valid from Junior role to Lead role. Your answer determine if you have JS basics clear or not.

What is High Order Function ?

By Definition - A Function which accepts a function as argument or returns a function as value or Both.

Most of the candidates answers half correctly. In order to qualify a function as High Order Function it has to pass one of the 3 criteria -

  1. Accept a function as a argument
  2. Return a function as a value
  3. Both of the above

Example of 1st Criteria :

Example of High order Function accepting function as argument

Here getUser function accepts onSuccess and onError. These are functions as argument. In this example, getUser does not return anything.

Example of 2nd Criteria :

Example of High order Function returning function as value

Here getSumFunc returns a new function as value.

Example of 3rd Criteria :

Example of High order Function accepting function as argument and returning function as value

Here getConfidentialFunction is a funtion which converts a normal function into a function with access to confidential data. Here getConfidentialFunction accepts a fucntion as argument but also returns a new function.

Top comments (0)