JavaScript Program to Pass a Function as Parameter

To understand this example, you should have the knowledge of the following JavaScript programming topics:


Example: Function as Parameter

 // program to pass a function as a parameter function greet() { return 'Hello'; } // passing function greet() as a parameter function name(user, func) { // accessing passed function const message = func(); console.log(`${message} ${user}`); } name('John', greet); name('Jack', greet); name('Sara', greet);

Output

 Hello John Hello Jack Hello Sara

In the above program, there are two functions: name() and greet().

  • The name() function takes two parameters.
  • The greet() function is passed as an argument to the name() function.

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges