Categories
JavaScript

Callback functions

Javascript callback functions explained.

As well as Python we’ll be adding semi regular tips and case studies of JavaScript code and features. In this article we’ll look at “callback” functions in JavaScript.

Let’s simplify them down to a very basic example which we show here:

                    
    function cbdemo(a, b, callback){
        console.log(callback(a,b));
    };
    var callback = function(a,b){
        return a * b;
    };
    var bx = cbdemo(2,3, callback);

                

The above will print to console.

We have a function calling another function inside itself.

Let’s use the response from the cbdemo function and display on the page

Now we get a nice bold “h2” number 6 viewable in our page in the browser!