DEV Community

Ramesh Vishnoi
Ramesh Vishnoi

Posted on • Edited on

6

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.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay