Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
JavaScript Program to Pass a Function as Parameter
In this example, you will learn to write a JavaScript program that will pass a function as a 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 thename()function.
Also Read:
Did you find this article helpful?