JavaScript Higher-Order Functions


In general, functions that operate on other functions, either by taking them as arguments or by returning them (or both), are called higher-order functions.

A higher-order function is a function that can take another function as an argument. You are using higher-order functions when passing callbacks.

function iAmCallbackFunction() {
    console.log("callback has been invoked");

function iAmJustFunction(callbackFn) {
    // do some stuff ...

    // invoke the callback function.

// invoke your higher-order function with a callback function.

A higher-order function is also a function that returns another function as its result.

function iAmJustFunction() {
    // do some stuff ...

    // return a function.
    return function iAmReturnedFunction() {
        console.log("returned function has been invoked");

// invoke your higher-order function and its returned function.