DEV Community

Discussion on: Explain Callback Like I'm Five

Collapse
 
nestedsoftware profile image
Nested Software • Edited

A callback is a function that you pass in as a variable to another function. A typical example in JavaScript is the setTimeout function. If you want something to execute after a delay, you can pass it as a callback to setTimeout.

function sayHello() {
    console.log('Hello there!');
}

setTimeout(sayHello, 1000);
Enter fullscreen mode Exit fullscreen mode

setTimeout will wait 1 second (1000 milliseconds) and then it will actually call sayHello. The result is that "Hello there!" will get printed to the console after one second.

Because it takes a function as an argument, setTimeout is called a "higher order function."

Collapse
 
rupeshgoud profile image
RupeshGoud

Thanks :)