Higher-Order Functions in JavaScript

kamesh sethupathi
2 min readNov 6, 2021

--

  • In javascript functional programming is powered by Higher-Order Functions.
  • A function that takes another function as its argument or returns a function is called a higher-order function
  • Let's assume, we are given an array of radius and we have to find its area, circumference, and diameter.

General implementation

  • The above code is absolutely correct, it satisfies our requirements.
  • But, there is a problem with it. We are repeating ourselves a lot. Other than the core logic there is no difference in the code.
  • It violates the concept of DRY ( Don’t repeat yourself)

Higher-order implementation

  • Now our code looks much cleaner and it becomes easy to add custom logic in the future.
  • Here the function calculate takes a callback logic function to perform the intended action.

JavaScript inbuilt functional support

  • If you look into the above code, the calculate function is pretty much doing the same thing what Array.map does.
  • You could see most of the JavaScript methods take a callback function, it's the core of functional programming.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

kamesh sethupathi
kamesh sethupathi

Written by kamesh sethupathi

Front-end developer | JS/React/Vue

No responses yet

Write a response