Pass Functions to Other Functions

In Change Outer Variables Carefully, recordVote() received the count through a parameter and handed the new count back. Every argument you’ve passed to a function so far has been data, such as a game, a name, or a number. Clicks need a different kind of argument. To make a button respond, your script hands the browser a function, and the browser calls it later.

In this lesson, you’ll write a function that takes another function and calls it, so you can watch both sides of that handover before the browser takes over the calling side. You’ll be able to tell a function apart from its result and say who calls a passed function, how often, and with what. Start with a loop Priya’s script already has.

Hand a Function to Another Function

Priya’s drawGames() loops over games and builds a line for each one. More jobs on her page need that same loop: logging the games while you test and picking out the quick ones. The for...of line is the same every time. Only the work inside the loop changes.

So write the loop once, and pass the work in. That’s possible because a function is a value. You saw this when the Console echoed describeGame without calling it. You can pass a function as an argument, like any other value. For a refresher on writing and calling functions, see Functions in JavaScript.

Open a new tab, go to about:blank, and open the Console with Ctrl+Shift+J, or Cmd+Option+J on macOS. Give it describeGame() again and three of Priya’s games, which is enough to see the pattern:

Language: JavaScript
> const describeGame = (game) => game.name + ": " +
…   game.minPlayers + "-" + game.maxPlayers +
…   " players, " + game.minutes + " minutes";
undefined
> const games = [
…   { name: "Catan", minutes: 90, minPlayers: 3,
…     maxPlayers: 4 },
…   { name: "Codenames", minutes: 15, minPlayers: 4,
…     maxPlayers: 8 },
…   { name: "Azul", minutes: 40, minPlayers: 2,
…     maxPlayers: 4 },
… ];
undefined

Now write the helper. forEachGame() takes a list of games and a function, and it calls that function once for each game, passing the game in:

Language: JavaScript
> const forEachGame = (games, callback) => {
…   for (const game of games) {
…     callback(game);
…   }
… };
undefined

Its second parameter, callback, holds a function. The line callback(game); calls that function with the current game as its argument. The first parameter, games, is the name forEachGame() gives to whatever list you pass, so inside the function, games means that list.

Next, you need a function to hand over. logGame() logs one game’s line. Write it, then pass it to forEachGame() without parentheses:

Language: JavaScript
> const logGame = (game) => console.log(describeGame(game));
undefined
> forEachGame(games, logGame)
Catan: 3-4 players, 90 minutes
Codenames: 4-8 players, 15 minutes
Azul: 2-4 players, 40 minutes
undefined

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.