You just need to remove the parenthesis:

addContact(entityId, refreshContactList);

This then passes the function without executing it first.

Here is an example:

function addContact(id, refreshCallback) {
    refreshCallback();
    // You can also pass arguments if you need to
    // refreshCallback(id);
}

function refreshContactList() {
    alert('Hello World');
}

addContact(1, refreshContactList);

You can also pass anonymous functions:

function addContact(id, refreshCallback) {
    refreshCallback();
    // You can also pass arguments if you need to
    // refreshCallback(id);
}

addContact(1, () => alert('hello world'));

Answer from Fenton on Stack Overflow
Top answer
1 of 16
1205

You just need to remove the parenthesis:

addContact(entityId, refreshContactList);

This then passes the function without executing it first.

Here is an example:

function addContact(id, refreshCallback) {
    refreshCallback();
    // You can also pass arguments if you need to
    // refreshCallback(id);
}

function refreshContactList() {
    alert('Hello World');
}

addContact(1, refreshContactList);

You can also pass anonymous functions:

function addContact(id, refreshCallback) {
    refreshCallback();
    // You can also pass arguments if you need to
    // refreshCallback(id);
}

addContact(1, () => alert('hello world'));

2 of 16
560

If you want to pass a function, just reference it by name without the parentheses:

function foo(x) {
    alert(x);
}
function bar(func) {
    func("Hello World!");
}

//alerts "Hello World!"
bar(foo);

But sometimes you might want to pass a function with arguments included, but not have it called until the callback is invoked. To do this, when calling it, just wrap it in an anonymous function, like this:

function foo(x) {
   alert(x);
}
function bar(func) {
   func();
}

//alerts "Hello World!" (from within bar AFTER being passed)
bar(function(){ foo("Hello World!") });

If you prefer, you could also use the apply function and have a third parameter that is an array of the arguments, like such:

function eat(food1, food2) {
    alert("I like to eat " + food1 + " and " + food2 );
}
function myFunc(callback, args) {
    //do stuff
    //...
    //execute callback when finished
    callback.apply(this, args);
}

//alerts "I like to eat pickles and peanut butter"
myFunc(eat, ["pickles", "peanut butter"]); 

๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ why is passing a function as a parameter a necessity in callback functions instead of just simply executing inside a function?
r/learnjavascript on Reddit: Why is passing a function as a parameter a necessity in callback functions instead of just simply executing inside a function?
July 19, 2023 - If you find yourself writing a function that only ever calls one specific callback function, then you probably don't need to pass it's reference as a parameter. For example, say you have a button that when a user clicks it should alert "Hello World" to the window.
Discussions

Basic JavaScript - Passing Values to Functions with Arguments - c49z7uqSwAiXrPuLhIIeT
Tell us whatโ€™s happening: Describe your issue in detail here. Im trying to understand how this is wrong when im doing it the way it was explained. Even after viewing the video i can see i did it the same way and im still wrong. Please help if anyone can? **Your code so far** function ... More on forum.freecodecamp.org
๐ŸŒ forum.freecodecamp.org
0
0
July 11, 2022
Can someone explain how to pass a function as an argument to another function in JS and show me a code example? I only know OOP and find it really confusing to do this.
This defines a function that doubles a number. function double (x) { return x * 2 } When this code is executed, a variable called double is created in the current scope and assigned the value of a function that when called, multiples 2 by its input and returns the result. To call a function, you can use parentheses (()). When placed at the end of a variable referring to a function, it invokes that function running the code inside. Any values inside the parentheses are passed into the function as inputs (in this case one, the x parameter) and anything the function returns is passed back to the point in the code at which the function was called. const result = double(3) // 3 becomes x in the function console.log(result) // logs: 6 If you refer to a function by its variable name without using parentheses, you'll get a reference to the function without calling it. This reference can be passed around just like any other value assigned to a variable. const alsoDouble = double // doesn't call, rather assigns double function to alsoDouble variable Any variable referring to a function can be called as a function. alsoDouble(3) // 6 Because functions can be treated just like any other variable, they can also be passed into functions as function arguments. In the examples so far, the value 3 was passed into the function where it became x inside the function. The same can be done with function values. This function takes two inputs, an array and a function. The function then calls the function it was passed on every value of the array allowing it (the callback) to change its value. function transform(array, callback) { for (let i = 0; i < array.length; i++) { const result = callback(array[i]) // calls the callback function passed to transform array[i] = result // assign back to array, transforming its value } } Using double with transform can allow us to double all the values in an array. const bets = [1, 5, 20] transform(bets, double) console.log(bets) // [2, 10, 40] callback inside transform acts just like alsoDouble from earlier - another variable holding the same function value as double only here the assignment is handled through a function call where it assigned a parameter variable (callback) to an argument (double) that the function was given when it was called. Because functions can be defined as expressions, you can also skip creating a variable before passing a function into another function. Instead, where you would have the variable of the function in the function call you can put the function definition directly. const bets = [1, 5, 20] transform(bets, function triple (x) { return x * 3 }) console.log(bets) // [3, 15, 60] Arrow functions provide a much shorter syntax for doing the same thing which are often preferred in this case const bets = [1, 5, 20] transform(bets, x => x * 3) console.log(bets) // [3, 15, 60] Most of the time, when using callbacks you're simply supplying the callback function and not defining the function using the callback. Common places where you'd be doing this include: // event listeners myButton.addEventListener('click', event => { alert("You just clicked my button!") }) // promises fetch(myUrl) .then(res => res.text()) .then(text => { myLoadedTextDiv.textContent = text }) // setTimeout setTimeout(() => { myButton.disabled = true myMessageDiv.textContent = "Too slow!" }, 5000) // array methods const myGrades = [100, 75, 80] const myGradesWithExtraCredit = myGrades.map(grade => { return grade + 5 }) console.log(myGradesWithExtraCredit) // [105, 80, 85] How the callback is called in all of these examples are built-in to those APIs. You don't see that. All you do is provide the function and it performs the call. More on reddit.com
๐ŸŒ r/learnjavascript
54
1
March 20, 2021
Why is passing a function as a parameter a necessity in callback functions instead of just simply executing inside a function?
If I'm understanding your question right, one reason it's done in order to write DRY code. If you find yourself writing a function that only ever calls one specific callback function, then you probably don't need to pass it's reference as a parameter. For example, say you have a button that when a user clicks it should alert "Hello World" to the window. const clickHandler = (callback) => { callback("Hello World"); } ... Somewhere in html Click me This is a pointless use of a callback. The button will do the same thing every single time so you could simply call console alert inside of the click handler. HOWEVER when things start to become more complicated, passing a reference of one function for another to call is a great way to make your code more flexible. Like maybe you have a bunch of buttons that you want to do different things. Maybe one of them alerts, one console.logs, one evaluates it's boolean statement and so on. Instead of having a bunch of different click handlers to handle those events, you can use the same handler and pass a different callback function const clickHandler = (callback) => { callback("Hello World"); } ... Somewhere in html Click me Click me Click me Click me Without callbacks you'd need a bunch of unique clickHandler functions for each button. In this button case that wouldn't be such a big deal, but when you're working on a real, complicated application, this level of abstraction saves a lot of development time and makes it very easy to modify your programs. More on reddit.com
๐ŸŒ r/learnjavascript
23
13
July 19, 2023
Pass Class as parameter that extends another class

If you want a class type (and not an instance of the class), you can use:

const foo = <T: {new(): Bar}>( baz: T )

This will accept derived classes too, as their constructors also produce something that can cast to Bar.

More on reddit.com
๐ŸŒ r/typescript
10
5
January 6, 2019
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ passing-a-function-as-a-parameter-in-javascript
Passing a function as a parameter in JavaScript - GeeksforGeeks
November 4, 2025 - Example 2: This example passes a function geeks_inner along with an argument 'Geeks!' to the function geeks_outer as an argument. ... function geeks_inner(value) { return 'hello ' + value; } function geeks_outer(a, func) { console.log(func(a)); } geeks_outer('Geeks!', geeks_inner); ... Example 3: Here in this example, a smaller function is passed as an argument in the sayHello function.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_function_parameters.asp
JavaScript Function Parameters
You can add as many parameters as you want, separated by commas. function fullName(firstName, lastName) { return firstName + " " + lastName; } let name = fullName("John", "Doe"); Try it Yourself ยป ยท In JavaScript, function parameters and arguments are distinct concepts: Parameters are the names listed in the function definition. Arguments are the real values passed to, and received by the function.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ examples โ€บ pass-a-function-as-parameter
JavaScript Program to Pass a Function as Parameter | Vultr Docs
May 15, 2025 - Passing a function as a parameter in JavaScript is a fundamental technique that enables developers to write flexible, reusable, and modular code. Since JavaScript supports first-class functions, it allows functions to be treated like variables, ...
๐ŸŒ
Mozilla
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Guide โ€บ Functions
Functions - JavaScript | MDN
When you pass an object as a parameter, if the function changes the object's properties, that change is visible outside the function, as shown in the following example:
Find elsewhere
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ examples โ€บ function-as-parameter
JavaScript Program to Pass a Function as Parameter
To understand this example, you should have the knowledge of the following JavaScript programming topics: ... // 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);
๐ŸŒ
freeCodeCamp
forum.freecodecamp.org โ€บ javascript
Basic JavaScript - Passing Values to Functions with Arguments - c49z7uqSwAiXrPuLhIIeT - JavaScript - The freeCodeCamp Forum
July 11, 2022 - Tell us whatโ€™s happening: Describe your issue in detail here. Im trying to understand how this is wrong when im doing it the way it was explained. Even after viewing the video i can see i did it the same way and im still wrong. Please help if anyone can? **Your code so far** function functionWithArgs(1, 2) { console.log(7 + 9); } functionWithArgs(3, 16); **Your browser information:** User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome...
๐ŸŒ
Kent C. Dodds
kentcdodds.com โ€บ blog โ€บ javascript-pass-by-value-function-parameters
JavaScript Pass By Value Function Parameters
March 23, 2021 - The answer is the fact that in JavaScript, when you call a function with arguments, the arguments you're passing are passed by value, not by reference.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-function-parameters
JavaScript Function Parameters - GeeksforGeeks
July 11, 2025 - You can assign default values to parameters if no arguments are provided. Allows capturing an indefinite number of arguments into an array. Primitive types are passed by value, whereas objects are passed by reference. ... Hello, Meeta! Parameter: name in the function definition.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javascript โ€บ javascript_function_parameters.htm
JavaScript - Function Parameters
In the below code, we have defined the 'val1' and 'val2' variables outside the function and passed them as a function argument. In the function body, we change the parameter value. In the output, you can see that even after updating the parameter value, the actual value of the 'val1' and 'val2' is not changed. <html> <head> <title> JavaScript - Arguments are passed by value </title> </head> <body> <p id = "output"> </p> <script> const output = document.getElementById("output"); function update(val1, val2) { val1 = 20; val2 = 30; } var val1 = "Hello"; var val2 = "World"; output.innerHTML += "Before calling the function!
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ javascript โ€บ how to pass parameter in javascript function?
How to Pass Parameter in JavaScript Function? - Scaler Topics
June 6, 2022 - When we call 'callingFunction' and pass 'anotherFunction' as argument, the function name - 'anotherFunction' gets assigned to the variable name 'functionToBeCalled' inside the 'callingFunction's body. Try console logging the parameter's value inside the callingFunction and see, I was not lying!
๐ŸŒ
SheCodes
shecodes.io โ€บ athena โ€บ 6412-passing-parameters-to-javascript-functions
[JavaScript] - Passing Parameters to JavaScript Functions - | SheCodes
Discover how to pass parameters to JavaScript functions when you call them and understand how to use the value of the passed parameter.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ can someone explain how to pass a function as an argument to another function in js and show me a code example? i only know oop and find it really confusing to do this.
r/learnjavascript on Reddit: Can someone explain how to pass a function as an argument to another function in JS and show me a code example? I only know OOP and find it really confusing to do this.
March 20, 2021 -

Can someone explain how passing a function as a parameter to another function works and what the syntax for it is? It's really confusing for a beginner in JS and someone who only knows OOPS.

Can you write a javascript code example where they pass a function as a parameter to another function and explain what's going on the syntax for it? Can't find any good / well-explained resources on it that are easy to understand.

Top answer
1 of 5
5
This defines a function that doubles a number. function double (x) { return x * 2 } When this code is executed, a variable called double is created in the current scope and assigned the value of a function that when called, multiples 2 by its input and returns the result. To call a function, you can use parentheses (()). When placed at the end of a variable referring to a function, it invokes that function running the code inside. Any values inside the parentheses are passed into the function as inputs (in this case one, the x parameter) and anything the function returns is passed back to the point in the code at which the function was called. const result = double(3) // 3 becomes x in the function console.log(result) // logs: 6 If you refer to a function by its variable name without using parentheses, you'll get a reference to the function without calling it. This reference can be passed around just like any other value assigned to a variable. const alsoDouble = double // doesn't call, rather assigns double function to alsoDouble variable Any variable referring to a function can be called as a function. alsoDouble(3) // 6 Because functions can be treated just like any other variable, they can also be passed into functions as function arguments. In the examples so far, the value 3 was passed into the function where it became x inside the function. The same can be done with function values. This function takes two inputs, an array and a function. The function then calls the function it was passed on every value of the array allowing it (the callback) to change its value. function transform(array, callback) { for (let i = 0; i < array.length; i++) { const result = callback(array[i]) // calls the callback function passed to transform array[i] = result // assign back to array, transforming its value } } Using double with transform can allow us to double all the values in an array. const bets = [1, 5, 20] transform(bets, double) console.log(bets) // [2, 10, 40] callback inside transform acts just like alsoDouble from earlier - another variable holding the same function value as double only here the assignment is handled through a function call where it assigned a parameter variable (callback) to an argument (double) that the function was given when it was called. Because functions can be defined as expressions, you can also skip creating a variable before passing a function into another function. Instead, where you would have the variable of the function in the function call you can put the function definition directly. const bets = [1, 5, 20] transform(bets, function triple (x) { return x * 3 }) console.log(bets) // [3, 15, 60] Arrow functions provide a much shorter syntax for doing the same thing which are often preferred in this case const bets = [1, 5, 20] transform(bets, x => x * 3) console.log(bets) // [3, 15, 60] Most of the time, when using callbacks you're simply supplying the callback function and not defining the function using the callback. Common places where you'd be doing this include: // event listeners myButton.addEventListener('click', event => { alert("You just clicked my button!") }) // promises fetch(myUrl) .then(res => res.text()) .then(text => { myLoadedTextDiv.textContent = text }) // setTimeout setTimeout(() => { myButton.disabled = true myMessageDiv.textContent = "Too slow!" }, 5000) // array methods const myGrades = [100, 75, 80] const myGradesWithExtraCredit = myGrades.map(grade => { return grade + 5 }) console.log(myGradesWithExtraCredit) // [105, 80, 85] How the callback is called in all of these examples are built-in to those APIs. You don't see that. All you do is provide the function and it performs the call.
2 of 5
2
A typical function call looks like this where you pass your name in and it prints out the name in a sentence. function sayMyName(name) { return "Hello my name is " + name; } sayMyName('George') // outputs -> "Hello my name is George" In this example we are simply passing name which is a string ("George"). Now what if instead of the string "George" we passed a function? function callMyName(nameFunction) { return nameFunction(); // `nameFunction` is referring to the `sayMyName` function below. } function sayMyName() { return "Hello my name is George"; } // we pass the function `sayMyName` as the parameter instead of `name` from the first example callMyName(sayMyName) // also outputs -> "Hello my name is George"
๐ŸŒ
DEV Community
dev.to โ€บ erikwhiting88 โ€บ passing-functions-as-parameters-in-javascript-339a
Passing Functions as Parameters in JavaScript - DEV Community
August 24, 2019 - In JavaScript, functions are considered "first class" or "higher order" functions. This is basically just a fancy way of saying we can pass functions as parameters to other functions, or even return functions from other functions.
๐ŸŒ
Medium
cmorinan.medium.com โ€บ passing-functions-as-arguments-in-javascript-tips-and-pitfalls-d29bbd4522a9
Passing functions as arguments in JavaScript โ€” tips and pitfalls | by Ciaran Morinan | Medium
October 30, 2018 - This involves specifying a function as a callback without naming the arguments to be passed to it โ€” the calling function will pass whatever values it has available as arguments, for the callback to deal with however it can.
๐ŸŒ
Medium
medium.com โ€บ coding-at-dawn โ€บ why-you-should-always-pass-objects-as-function-parameters-in-javascript-7fb7c5833dc6
Why You Should Always Pass Objects as Function Parameters in JavaScript | by Dr. Derek Austin ๐Ÿฅณ | Coding at Dawn | Medium
January 6, 2023 - Why You Should Always Pass Objects as Function Parameters in JavaScript This code pattern, which is commonly used in React, is a much better idea than using traditional function parameters in JS โ€ฆ
๐ŸŒ
LogRocket
blog.logrocket.com โ€บ home โ€บ how to pass a typescript function as a parameter
How to pass a TypeScript function as a parameter - LogRocket Blog
May 15, 2025 - This article explores how to pass functions and structured objects as parameters in TypeScript. It highlights use cases, syntax differences, and practical scenarios where each may be preferred, especially when working with function types, inheritance, excess property checks, and optional fields. In JavaScript...
๐ŸŒ
LaunchCode
education.launchcode.org โ€บ intro-to-professional-web-dev โ€บ chapters โ€บ more-on-functions โ€บ passing-functions.html
11.3. Passing Functions as Arguments โ€” Introduction to Professional Web Development in JavaScript documentation
Functions are data, and therefore can be passed around just like other values. This means a function can be passed to another function as an argument. This allows the function being called to use the function argument to carry out its action.
๐ŸŒ
Go Make Things
gomakethings.com โ€บ my-preferred-way-to-pass-arguments-into-a-function-with-vanilla-javascript
My preferred way to pass arguments into a function with vanilla JavaScript | Go Make Things
November 24, 2021 - Inside the function, you can use object destructuring to get individual parameter values from it. function wizard (args) { // Get the argument values let {name, job, spell, isEvil} = args; } I like to pair this approach with the Object.assign() ...