A callback function is a function that is passed as an argument to another function and is executed after the completion of that function.
In this example, the callback function is executed immediately after the main function finishes execution:
Output:
In this example, the callback is used to handle an asynchronous operation:
Output:
sayGoodbye
is a synchronous callback — it gets executed immediately after greet
finishes.processData
is an asynchronous callback — it waits for setTimeout
to complete before executing.