How To Use Callback Function Using JavaScript?


Share:
Published By: Admin, Published On: Nov 18, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to use callback function example.We will check how to use callback function. This is a short guide on use callback function in javascript. Here you will learn how to use javascript callback function. Let's get started with how to use callback function in javascript.

Here i will give you many example how to use callback function using javascript.

Example : 1
<html>
    <head>
        <title>How To Use Callback Function Using JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
        <h3>How To Use Callback Function Using JavaScript? - MyWebtuts.com</h3>
        <p> The getData() function is called its arguments and the callback is executed after the completion of getData() function. </p>
        <script>
            function gData(a, b, callback){
                document.write(" The multiplication of the numbers " + a + " and " + b + " is : " + (a*b) + "<br><br>" );
                callback();
            }
            function sData(){
                document.write(' This is the showData() method execute after the completion of getData() method.');
            }
            gData(25, 5, sData);
        </script>
    </body>
</html>
Output :
The getData() function is called its arguments and the callback is executed after the completion of getData() function.

The multiplication of the numbers 25 and 5 is : 125

This is the showData() method execute after the completion of getData() method.
Example : 2
<html>
    <head>
        <title>How To Use Callback Function Using JavaScript? - MyWebtuts.com</title>
    </head>
    <body>
    <h3>How To Use Callback Function Using JavaScript? - MyWebtuts.com</h3>
    <h2> This is the MyWebtuts.com </h2>
        <script>
            function sData(nm, amt) {
                alert(' Hello ' + nm + '\n Your entered amount is ' + amt);
            }

            function gData(callback) {
                var nm = prompt(" Welcome to the MyWebtuts.com \n What is your name?");
                var amt = prompt(" Enter some amount...");
                callback(nm, amt);
            }
            gData(sData);
        </script>
    </body>
</html>
Output :
Welcome to the MyWebtuts.com
What is your name?
abc
Enter some amount...
25000
Hello abc 
Your entered amount is 25000

It will help you...

Share: