Higher Order Function

Higher order functions are simply a function that can take a function as an argument or a function that return another function.

function authenticate(verify) {
    if (verify) {
        return "you are verified"
    }
    return "you are not verified"

}
function person(person, fn) {
    if (person.level === 'admin') {
        return fn(true);
    }
    return fn(false);
}

console.log(person({ level: 'admin', name: "jay" }, authenticate));
you are verified

with higher order function is the ability to tell the function what to do during invocation. we are able to have more flexibility. you are able to keep our code dry and a lot more flexible.

More Example

const multiplyBy = function (num1) {
    return function (num2) {
        return num1 * num2;
    }
}

const multiplyByFive = multiplyBy(5)

console.log(multiplyByFive(2));
console.log(multiplyByFive(3));
10
15
  • Again Higher order functions are simply a function that can take a function as an argument or a function that return another function.
  • The main benefit of Higher order functions is to keep our code dry and not repeating ourselves.

Leave a Reply

Your email address will not be published.

ANOTE.DEV