javascript

Pass a function as parameter of another function in Javascript

In javascript, if you have a function that executes a block of code and you want to pass this function as a parameter of another function. You can use the code examples explained here.

function message() {
    return "Devsheet";
}

function print_message(my_function) {
    var msg = my_function();

    console.log(`Hello from ${msg}`);
}

print_message(message);

Output

Hello from Devsheet
Was this helpful?