CSE 121B: JavaScript Language

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

Check Your Understanding

Given these function declarations:

function calculate(a, b, callback) {
  callback(a + b);
}

function displayResult(result) {
  console.log('The result is: ' + result);
}