Search code snippets, questions, articles...

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);
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place.

Output

Hello from Devsheet
Was this helpful?
0 Comments