Callback Function in JavaScript -

 


Callback OR Higher Order Function in JavaScript:

Callbacks are simply Functions In JavaScript which are to be called and then executed after the execution of another function has finished. So how it happens? Actually, In JavaScript, functions are itself considered as objects and hence as all other objects, even functions can be sent as arguments to other functions. The most common and generic use case one can think of is setTimeout() function in JavaScript.


Consider the following Example of setTimeout() in JavaScript and hence try to get hands-on Callbacks JavaScript gave to us by default.

        <script>
       
        //callback function
        function abc(val){         //val()
        console.log(val(),"this is normal function");
        }

        function callback(){
        console.log("this is callback function");
        return 'called'
        }

        abc(callback);

        </script>