How to write a function that accepts other functions as parameter

How to write a function that accepts other functions as parameter

by cybersal

A function that can take another function as a parameter is referred to as a “higher-order function” in various programming languages.

Here is a JavaScript illustration:

function acceptFunc(func) {
return func();
} 

function sayHello() {
return "Hello!";
}
console.log(acceptFunc(sayHello)); // Output: "Hello!"

The acceptFunc function in the above example accepts the function func as a parameter.
The sayHello function is passed as an input when calling the acceptFunc function, and this function returns “Hello!” as the output of calling func.

Related Articles

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

Privacy & Cookies Policy