Callback Functions
Overview
Callback functions are a powerful feature of JavaScript. They are used extensively in the JavaScript language and in the browser. Callback functions are used to handle events, to process data, and to control the flow of a program.
"A callback function is a function passed into another function as an argument, which is then
invoked
inside the outer function to complete some kind of routine or action." - MDN Web Docs
Prepare
- Read 📝Callback Functions - MDN web docs
- Ponder this article demonstrating the use of callback functions: Callback with built-in functions - devgenius.io
Check Your Understanding
Given these function declarations:
function calculate(a, b, callback) {
callback(a + b);
}
function displayResult(result) {
console.log('The result is: ' + result);
}
- Call a function named
calculate
and pass it the arguments to support the console output of the following equation:2 + 3
.💡Hint: You need to pass three arguments to thecalculate
function.Answer
calculate(2, 3, displayResult)