A callback is a function that will be executed after another function gets executed.
Functions that are used as an argument to another function are called callback functions.
console.log(Math.floor(sum / 2));
console.log(sum * 2);
var sum = num1 + num2;
operationOnSum(3, 3, divideByHalf); // Outputs 3
operationOnSum(5, 5, multiplyBy2); // Outputs 20
In the code above, we are performing mathematical operations on the sum of two numbers.
The operationOnSum function takes 3 arguments, first number, second number, and the operation that is to be performed on their sum (callback) .
Both divideByHalf and multiplyBy2 functions are used as callback functions in the code above.
These callback functions will be executed only after the function operationOnSum is executed.
Therefore, callback is a function that will be executed after another function gets executed.