Updated April 14, 2023
Introduction to JavaScript Callback Function
JavaScript Callback function are the most special and important function of JavaScript whose main aim is to pass another function as a parameter where the callback function runs which means one function when infused into another function with the parameters is again called as per the requirement. The parameters passed by the function at the time of invocation must be kept in mind by passing otherwise it might create compilation error at the time of calling the function. Callback function in JavaScript can be called in two ways namely synchronously or asynchronously.
Syntax:
function func_One(p)
{
return p;
};
function func_two(vr1)
{
// Program Body
}
function func_two(func_One);
The syntax flow is as follows where the function func_One is the first function which passes the parameter p to the function and then returns the value for the first function. Simultaneously a second function is called where the second function passes the parameter as vr1 which gets infused with the second function as per the requirement and then it goes for the program body and after coming out of it calls for the second function which in this case is func_two(func_One) which signifies that it is using the callback function in JavaScript.
How Callback Function Works in JavaScript?
- Callback Function in JavaScript has two functions which plays its role interchangeably as this method of passing function to another function is possible in the JavaScript with the help of libraries and the scope is also not limited which means it can be used and the callback function in JavaScript can be performed within the entire code snippet in anywhere and anytime. Just the important thing to keep in mind is the parameters passed they should match the requirement and then passed otherwise it may throw kind of exception or error at the time of execution.
- In other programming languages also the concept of calling the function behaves in a similar fashion and is common for all the functions passed with the name as a callback function. It all depends on the parameter and the invocation of the function like when it will be called by another function and based on that it can be decided whether the function calling is performed in a synchronous or asynchronous way. As described above in the syntax flow how it works is like function one which is the first function considers one argument and gives an alert with p passing as its argument. Consequent to which the second function takes in one argument and a function.
- Then the second function passes the argument it took for the function to take in the value. Therefore, if we investigate detail the syntax flow one overview can be made clearly that the last function which is the Function one in this case is the callback function. It is also possible to pass an anonymous function as a variable to the function which in turns make the variable assigned to the next function and then in that case the callback function gets defined when we call for the second function again the callback function mechanism with anonymous function passing a variable mechanism.
- One very promising feature of the JavaScript callback function is that the callback function inside the second function can be called as many numbers of times as we want. There is no constraint and it can be called as many numbers of times as required. Another feature of this function is that it is possible to pass as many numbers of functions we want inside another function.
- With the synchronous and asynchronous callback function mechanism, it is possible to perform many more useful functionalities like error handling and then making the transitions for the type of function calling easier and more compatible for the programmers to work efficiently with the callback mechanism environment. Mostly the working environment supports for the asynchronous actions which involve that the action will initiate now and then they will finish later once the entire initiated action gets covered and finish then the asynchronous functionality comes into the picture with full functionality.
Types of JavaScript Callback Function
Below are the types of JavaScript Callback Function:
- Synchronous Callback Function
- Asynchronous Callback Function
1. Synchronous Callback Function
As its name suggests Callback function are said to synchronous callback function if the code in execution gets executed in a sequence without any halt or hindrance and gives the desired output smoothly formulated asynchronous callback function.
Below are the examples:
This program demonstrates that the number provided in the given array needs to be arranged in ascending without any halt which signifies successful execution of Synchronous Callback function.
Code:
let sm_num = [12,15,10,5,9,1,6];
sm_num.sort((l, n) => l - n);
console.log(sm_num);
Output:
2. Asynchronous Callback Function
Asynchronous Callback Function is a kind of function where the JavaScript which contains the program logic needs to wait for completing the rest of the code in execution prior to which it will execute the next set of code while waiting. Also, JavaScript involves single threading which means that the threads responsible for performing the operations involve callback queuing and loop back for events. Therefore, when the callback function is still in execution after the wait are called as asynchronous callback function religiously.
Below are the examples:
This program is used to demonstrate the asynchronous callback function where the callback function calls the function after performing a wait of a certain time period once queued and is shown in the output.
function down_load(link, callback) {
setTimeout(() => {
console.log(`Download the link ${link} ...`);
callback(link);
}, 2000);
}
let link = 'https://educba.com';
down_load(link, function(data) {
console.log(`Check the Data ${data}`);
});
Output:
Conclusion
JavaScript is a well-formed and preferred language by most programmers because of its simplicity and flexibility so do JavaScript Functions when incorporated makes the entire coding experience user-friendly with respect to the view and browser compatibility. It lets users handle errors and can use the same function again and again without re-writing the entire function from scratch.
Recommended Articles
This is a guide to JavaScript Callback Function. Here we discuss the Introduction to JavaScript Callback Function and how it works along with Examples and Code Implementation. You can also go through our other suggested articles to learn more –