Friday, October 24, 2025
HomeLanguagesJavascriptPassing a function as a parameter in JavaScript

Passing a function as a parameter in JavaScript

In this article, we will pass a function as a parameter in JavaScript. Passing a function as an argument to the function is quite similar to passing a variable as an argument to the function. So variables can be returned from a function. 

The below examples describe passing a function as a parameter to another function. 

Example 1: This example passes a function neveropen_inner to the function neveropen_outer as an argument. 

Javascript




function neveropen_inner(value){
    return 'hello User!';
}
function neveropen_outer(func){
    console.log(neveropen_inner());
}
 
neveropen_outer(neveropen_inner);


Output

hello User!

Example 2: This example passes a function neveropen_inner along with an argument ‘Geeks!’ to the function neveropen_outer as an argument. 

Javascript




function neveropen_inner(value) {
    return 'hello ' + value;
}
function neveropen_outer(a, func) {
    console.log(func(a));
}
 
neveropen_outer('Geeks!', neveropen_inner);


Output

hello Geeks!

Example 3: Here in this example, a smaller function is passed as an argument in the sayHello function. So here we are passing a smaller function address to the function sayHello.

Javascript




function sayHello(param) {
    console.log("hello", param);
    param();
    return "Hiii Geeks for Geeks"
}
 
// Function address
function smaller() {
    console.log("Is everything alright")
}
 
// Function call
const returnHello = sayHello(smaller)
console.log(returnHello)


Output

hello [Function: smaller]
Is everything alright
Hiii Geeks for Geeks
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS