Callback Functions

A callback function is a function that is passed as an argument to another function.

This can either be as a function reference, anonymous function or arrow function

alt

It's important to note, a callback function is not necessarily called

JS

Copy

function performAction(action) {
    action()
}

function sayHello() {
    console.log("Hello, World!")
}

performAction(sayHello) // Hello, World!

Above is an example where the callback function is passed as a function reference.

JS

Copy

function performAction(action) {
    action()
}

performAction(function () {
    console.log("Hello, World!")
}) // Hello, World!

Above is an example where the callback function is passed as a anonymous function.

JS

Copy

function performAction(action) {
    action()
}

performAction(() => console.log("Hello, World!")) // Hello, World!

Above is an example where the callback function is passed as an arrow function