This site is mobile accessible. Press the "Tap Here" button to use a different font-size.


Smartphone icons created by Freepik - Flaticon

  • 13.1 JS Callbacks

    • "I will call back later!"
    • A callback is a function passed as an argument to another function
    • This technique allows a function to call another function
    • A callback function can run after another function has finished

    Function Sequence

    • JavaScript functions are executed in the sequence they are called. Not in the sequence they are defined.
    • This example will end up displaying "Goodbye":
    Example 1: JS Functions - Function Sequence part 1
    function myFirst() {
      myDisplayer("Hello");
    }
    
    function mySecond() {
      myDisplayer("Goodbye");
    }
    
    myFirst();
    mySecond();

    This example will end up displaying "Hello":

    Example 2: JS Functions - Function Sequence part 2
    function myFirst() {
      myDisplayer("Hello");
    }
    
    function mySecond() {
      myDisplayer("Goodbye");
    }
    
    mySecond();
    myFirst();

    Sequence Control

    • Sometimes you would like to have better control over when to execute a function.
    • Suppose you want to do a calculation, and then display the result.
    • You could call a calculator function (myCalculator), save the result, and then call another function (myDisplayer) to display the result:
    Example 3: JS Functions - Function Sequence part 3
    function myDisplayer(some) {
      document.getElementById("demo").innerHTML = some;
    }
    
    function myCalculator(num1, num2) {
      let sum = num1 + num2;
      return sum;
    }
    
    let result = myCalculator(5, 5);
    myDisplayer(result);

    Or, you could call a calculator function (myCalculator), and let the calculator function call the display function (myDisplayer):

    Example 4: JS Functions - Function Sequence part 4
    function myDisplayer(some) {
      document.getElementById("demo").innerHTML = some;
    }
    
    function myCalculator(num1, num2) {
      let sum = num1 + num2;
      myDisplayer(sum);
    }
    
    myCalculator(5, 5);

    JavaScript Callbacks

    • A callback is a function passed as an argument to another function.
    • Using a callback, you could call the calculator function (myCalculator) with a callback, and let the calculator function run the callback after the calculation is finished:
    Example 5: JS Functions - Callback Functions
    function myDisplayer(some) {
      document.getElementById("demo").innerHTML = some;
    }
    
    function myCalculator(num1, num2, myCallback) {
      let sum = num1 + num2;
      myCallback(sum);
    }
    
    myCalculator(5, 5, myDisplayer);
    • In the example above, myDisplayer is the name of a function.
    • It is passed to myCalculator() as an argument.
    • When you pass a function as an argument, remember not to use parenthesis.
    • Right: myCalculator(5, 5, myDisplayer);
    • Wrong: myCalculator(5, 5, myDisplayer());

    When to Use a Callback?

    • The examples above are not very exciting.
    • They are simplified to teach you the callback syntax.
    • Where callbacks really shine are in asynchronous functions, where one function has to wait for another function (like waiting for a file to load).
    • Asynchronous functions are covered in the next chapter.
    Navigate this module

    Eventually the navigation links, above, will be replaced by these << (previous) and >> (next) buttons below.



    JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon