JavaScript callback17 Mar 2025 | 3 min read A callback function can be defined as a function passed into another function as a parameter. Don't relate the callback with the keyword, as the callback is just a name of an argument that is passed to a function. In other words, we can say that a function passed to another function as an argument is referred to as a callback function. The callback function runs after the completion of the outer function. It is useful to develop an asynchronous JavaScript code. In JavaScript, a callback is easier to create. That is, we simply have to pass the callback function as a parameter to another function and call it right after the completion of the task. Callbacks are mainly used to handle the asynchronous operations such as the registering event listeners, fetching or inserting some data into/from the file, and many more. Now, let's see how to create a callback by using some illustrations. It is an example of an asynchronous callback. Asynchronicity can be defined as if JavaScript has to wait to complete the operation and execute the rest of the program during waiting. Example1In this example, there are two functions getData( x, y, callback) and showData(). Here, we are calling the getData() with the showData(); that is, we are passing it as the third argument of the getData() function along with two parameters. As a result, the getData() is invoked with the specified parameters, including the callback. The getData() function display the multiplication of two numbers and once it gets completed the callback function will get executed. In the output, we can see the data of the showData() function gets printed after the output of getData() function. Output ![]() Callbacks are generally used to continue the execution after completing an asynchronous operation - such are referred to as the asynchronous callbacks. Now, in the next example, we will see a callback which gets immediately executed. Example2It is another example of using callbacks. It is an example of synchronous callback that gets immediately executed. Here, there are two functions getData(callback), which takes the input from the user using the prompt box, and the function showData(name, amt), which displays the data entered by the user using the alert dialog box. Output After the execution of above code, a prompt box will be displayed asking the name of the user - ![]() After entering the name, when the user clicks OK, then another prompt box will be displayed asking the amount to be entered - ![]() After entering the amount when the user clicks OK, then an alert box will be displayed. It shows the entered user name and amount. ![]() Next TopicJavaScript closures |
In this tutorial, we will learn, discuss, and understand the JavaScript form. We will also see the implementation of the JavaScript form for different purposes. Here, we will learn the method to access the form, getting elements as the JavaScript form's value, and submitting the form. Introduction...
3 min read
The JavaScript date object can be used to get a year, month and day. We can display a timer on the webpage with the help of a JavaScript date object. There are many types of date formats in JavaScript: ISO Date, Short Date, and Long Date....
3 min read
Redirect is nothing but a mechanism of sending search engines and users on a different URL from the original one. The redirected page can be on the same server or on a different server. It can also be on the same website or on different...
3 min read
Object The object is the type of collection that allows us to store weakly held objects. Unlike Set, the WeakSet are the collections of objects only. It doesn't contain the arbitrary values. Syntax new WeakSet([iterable]) Parameter iterable - It represents the iterable object whose elements will be added...
1 min read
Object The object is a type of collection which is almost similar to Map. It stores each element as a key-value pair where keys are weakly referenced. Here, the keys are objects and the values are arbitrary values. Syntax new WeakMap([iterable]) Parameter iterable - It represents an array...
1 min read
? We will comprehend how to add a property to an object in JavaScript in this article. JavaScript Object The JavaScript object is a group of properties in the form of key-value pairs. Syntax: const obj = { property1: value1, property2: value2, property n: value3, }; ...
4 min read
The defer is a Boolean value, used to indicate that script is executed after the document has been parsed. It works only with external scripts (i.e., works only when we are specifying the src attribute in <script> tag). It declares that the script will not...
2 min read
/wait The programming languages such as PHP and C has a sleep(sec) function to pause the execution for a fixed amount of time. Java has a thread.sleep(), python has time.sleep(), and GO has time.sleep(2*time.second). Unlike other languages, JavaScript doesn't have any sleep() function. We can use some...
3 min read
In this article, we will see how to calculate the difference between two dates by using JavaScript. If we use the right methods, then the technique of calculating the difference is straightforward. The date object is required to calculate the difference between the dates in JavaScript....
3 min read
The object illustrates an array like view of an underlying binary data buffer. There are many number of different global properties, whose values are TypedArray constructors for specific element types, listed below. Types of TypedArray Int8Array Size in bytes: 1 Description: 8-bit two's compliment signed integer. Type: byte. Value Range:...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India