Skip to content
techsnow

Functions

Callbacks

A callback is a function passed to another function, to be called later. You have already seen them in event listeners and array methods.

function doTwice(action) {
  action();
  action();
}

doTwice(() => console.log("Hi!"));

Output

Hi!
Hi!
setTimeout(() => {
  console.log("This runs after 2 seconds");
}, 2000);