How to Pass a Function as an Argument in JavaScript
In JavaScript a function is a value, like a number or a string, so you can hand it to another function. Write its name without brackets, say('Hello', shout), and the other function calls it when it needs to. A function passed this way is often called a callback.
Syntax
function <receiver>(<parameter>) {
<parameter>(<arguments>);
}
<receiver>(<function name>);
| Part | What it does |
|---|---|
<receiver> | The function that takes another function as an argument. |
<parameter> | The name the receiver uses for the function it's given. |
<parameter>(<arguments>) | Calls the function it was given, with brackets, like any other function. |
<function name> | The function you pass, by its name without brackets. With brackets you'd call it on the spot and pass what it returns instead. |
MDN's Functions guide describes functions as values you can pass around like any other.
The Example
The example has two small functions that change text, shout and whisper, and a function say that takes some text and a function, and logs what the function makes of the text. It calls say twice, with a different function each time, then prints the type of shout.
Prerequisites
- Node.js. This guide uses Node.js 26.13.1.
Step 1: Write the Code
Create a file named main.js:

function shout(text) {
return text.toUpperCase() + '!';
}
function whisper(text) {
return text.toLowerCase() + '...';
}
function say(text, style) {
console.log(style(text));
}
say('Hello', shout);
say('Hello', whisper);
console.log(typeof shout);
say doesn't know which function it will get. It calls whatever arrives in style.
Step 2: Run It
node main.js
HELLO!
hello...
function

The command is the same in zsh, bash and PowerShell.
What Happened
say('Hello', shout)printedHELLO!. Insidesay,stylewas theshoutfunction, sostyle(text)ranshout('Hello').say('Hello', whisper)printedhello.... Samesay, different function, different result.typeof shoutis'function'. The nameshoutwithout brackets is the function itself, a value you can store or pass on.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Adding brackets, say('Hello', shout()) | shout runs at once with no text and throws TypeError: Cannot read properties of undefined (reading 'toUpperCase'), before say even starts | Pass the name only: say('Hello', shout) |
Passing the name as a string, say('Hello', 'shout') | TypeError: style is not a function | Pass the function itself, without quotes |
Forgetting return in the function you pass | The receiver gets undefined back, and logs undefined | Return the result |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: ... is not a function | What you passed isn't a function: a string, undefined, or the result of a call | Pass the function's name without brackets or quotes |
| The function runs too early, or only once | You passed shout() rather than shout | Remove the brackets |
undefined where you expected a result | The function you passed has no return | Add return |
FAQ
Can I write the function right there, without a name?
Yes. say('Hello', function (text) { return text + '?'; }) printed Hello?. The shorter arrow form does the same: say('Hello', (text) => text + '?') printed Hello? too.
Do built-in functions take functions as well?
Many do. ['a', 'b'].map(shout) calls shout for each item and returned [ 'A!', 'B!' ], and setTimeout calls the function you give it after a delay. In a web page, addEventListener takes the function to run when an event happens, such as a click.
Why is it called a callback?
Because the receiver "calls back" your function when it's ready: right away, like say, or later, like setTimeout.
Conclusion
To pass a function, write its name without brackets, and call it inside the receiver with the parameter's name and brackets. If you see "is not a function" or a function running too early, look for brackets or quotes where only the name belongs.
Additional Resources
- Functions, in MDN's JavaScript Guide.
- Callback function, in MDN's glossary.
- Array.prototype.map(), on MDN.
