Skip to main content

How to Pass a Function as an Argument in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
function <receiver>(<parameter>) {
<parameter>(<arguments>);
}

<receiver>(<function name>);
PartWhat 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:

main.js, which defines shout, which returns its text in capitals with an exclamation mark, whisper, which returns it in lowercase with three dots, and say, which logs style of text, then calls say with Hello and shout, say with Hello and whisper, and logs typeof shout.

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​

Terminal
node main.js
Output
HELLO!
hello...
function

A zsh terminal where node main.js prints HELLO! with an exclamation mark, hello followed by three dots, and function.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • say('Hello', shout) printed HELLO!. Inside say, style was the shout function, so style(text) ran shout('Hello').
  • say('Hello', whisper) printed hello.... Same say, different function, different result.
  • typeof shout is 'function'. The name shout without brackets is the function itself, a value you can store or pass on.

Common Mistakes​

MistakeWhat happensDo 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 startsPass the name only: say('Hello', shout)
Passing the name as a string, say('Hello', 'shout')TypeError: style is not a functionPass the function itself, without quotes
Forgetting return in the function you passThe receiver gets undefined back, and logs undefinedReturn the result

Troubleshooting​

SymptomLikely causeFix
TypeError: ... is not a functionWhat you passed isn't a function: a string, undefined, or the result of a callPass the function's name without brackets or quotes
The function runs too early, or only onceYou passed shout() rather than shoutRemove the brackets
undefined where you expected a resultThe function you passed has no returnAdd 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​