Higher-Order Function in JavaScript

Level 3127 keystrokes

Higher-order functions return other functions, enabling function factories.

JavaScript code

// Define a function that returns another function
// This is called a higher-order function
1function createMultiplier(factor) {
// Return an arrow function that multiplies by factor
// The returned function is a closure: it remembers factor
2 return (num) => num * factor;
3}
// Create a function that doubles numbers
4const double = createMultiplier(2);
// Call the created function: 5 * 2 = 10
5console.log(double(5));

Output

10
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • (× 5
  • )× 5
  • ;× 3
  • =× 2
  • .× 1
  • {× 1
  • }× 1
  • *× 1
  • >× 1

More JavaScript examples at level 3

← JavaScript typing practice