Skip to content

Latest commit

 

History

History
26 lines (18 loc) · 559 Bytes

higher-order-function.md

File metadata and controls

26 lines (18 loc) · 559 Bytes

Higher Order Function

Function that accepts function as their argument or return any function as a result are called as higher order function. Such functions are useful for applying wrapper logic over existing functions.

function compose(f, g) {
  return function (x) {
    return f(g(x));
  };
}

function square(x){
    return x * x;
}

function squareRoot(x){
    return Math.sqrt(x)
}

const identity = compose(squareRoot, square);

identity(7) // 7

Here compose function accept functions are it's argument and return a function as a result.