Sikau

Advanced JavaScript · Day 3 · about 25 min

Higher-order functions

Functions in, functions out, and composing them.

Building with small pieces

map, filter and addEventListener all take functions. A function that takes or returns another function is a higher-order function. With them you can build new behaviour by snapping small functions together.

Writing your own

function myMap(list, fn) {
  const out = [];
  for (const item of list) out.push(fn(item));
  return out;
}
console.log(myMap([1, 2, 3], (n) => n * 10));

const not = (fn) => (...args) => !fn(...args);
const isEven = (n) => n % 2 === 0;
console.log([1, 2, 3, 4].filter(not(isEven)));

The rest of this lesson

  • Composition: pipelines of functionsexplain
  • Functions that make functionsexplain
  • Predict the outputpredict
  • Pipe itpredict
  • Your turnwrite code
  • Challengechallenge
  • Badge quizquiz
  • What you learnedrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 27 lessons of Advanced JavaScript