Tsallaka zuwa abun ciki
Justice Tech School

Callbacks and Higher-Order Functions

Functions are values, so you can pass one to another function. The function you pass is a callback, and the function that receives it is a higher-order function. map, filter, forEach and addEventListener all work this way.

Example
function repeat(times, action) {
  for (let i = 1; i <= times; i++) {
    action(i);
  }
}
repeat(3, (n) => console.log("Run " + n));

const words = ["a", "bb", "ccc"];
words.forEach((w) => console.log(w.length));

Exercise

Run the callback.

function run(cb) {
  ();
}

Quiz Test

What is a callback?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.