Ads Top

What is callback function in javascript

Callback function: when finished main function after then executes another function because callback we use as an argument or parameter. It defines the main function as a variable and
passed in the argument scope. JavaScript callback function example in the below:

<script>


    var passWord = '';
        function login(aa) {
            passWord = document.getElementById("pName").value;
            // Here define this is a function
            aa();
        }

        function validations() {
               alert('Thanks >>>>>>>>>> '+ passWord);
        }
        
</script>

<div style="max-width:500px; margin:100px auto;"><input id="pName" type="text">
<button onclick="login(validations)">Check</button>

 
We have called validations function in the login function and showing value in the alert box.

Why are we use the callback function?
It's required where we are waiting for the response from the server when got response then gets value and execute it.

No comments:

Copyright Reserved to Anything Learn. Powered by Blogger.