What is a Callback function?
A Callback function is a function that is gonna get called at a later point in time.
const btnAdd = document.getElementById('btnAdd');
btnAdd.addEventListener('click', function clickCallback(e) {
// do something useless
});
In this example, we wait for the click event
in the element with an id of btnAdd, if it is clicked
, the clickCallback
function is executed. A Callback function adds some functionality to some data or event. The reduce
, filter
and map
methods in Array expects a callback as a parameter. A good analogy for a callback is when you call someone and if they don't answer you leave a message and you expect them to callback. The act of calling someone or leaving a message is the event or data and the callback is the action that you expect to occur later.