Waiting Answer May 18, 2024

Is It Possible to Pass Function as Parameter in JS

I need to pass a function as a parameter in another function

Answers
2024-06-09 18:04:16

Yes, it is possible to pass a function as a parameter in JavaScript. This is a common feature in JavaScript and is frequently used in higher-order functions, callback functions, and event handlers.

function greet(name) {

    return `Hello, ${name}!`;

}

 

function displayGreeting(greetingFunction, name) {

    console.log(greetingFunction(name));

}

 

displayGreeting(greet, 'Alice'); // Outputs: Hello

, Alice!