1.

What do you understand by high order function in javascript?

Answer»

The basic definition of High order function is a function is called high order function if it either accept a function as an argument or returns another function. But now let’s talk about the benefit of writing a high order function. In javascript, we are TRYING to write less amount of code and try to reuse the same code or functionality at many times as we can. High order function way of writing code allows us to IMPLEMENT currying in Javascript. Now, what is this currying? Currying is a way of writing code where you write a common functionality and change the functionality via argument. Let me give you an example:

function multiplyBy(a) { return function(b) { return a * b; }; }

This function is high order function as this returns a new function. This function will act as function generator for different functionalities. For example, I can create a double function by using the above high order function:

const double = multiplyBy(2); console.log(double(4)) //result: 8

Similarly, I can use the same high order function to generate a triple function which takes the number as argument and return three times the number.

const triple = multiplyBy(3); console.log(triple(4)) //result: 12

This type of function generation is known as currying in javascript. It is a BETTER way of writing code as this decouples the base functionality with final LOGIC. The base logic can be re-used at multiple PLACES.



Discussion

No Comment Found