How to Create a Custom Callback in JavaScript? Last Updated : 08 Oct, 2024 Comments Improve Suggest changes Like Article Like Report A callback is a function that executes after another function has been completed in JavaScript. As an event-driven language, JavaScript does not pause for a function to finish before moving on to the next task. Callbacks enable the execution of a function only after the completion of another, making them essential for managing asynchronous operations. Since all JavaScript functions are objects, they can be passed as arguments to other functions. Many built-in functions utilize callbacks, and custom callback functions can be created by defining a callback parameter. Optionally, the typeof operator can be used to verify that the passed argument is indeed a function.Syntax:function processThis(message, callback) { console.log("Running function first with message: " + message); if (typeof callback == "function") callback(); } processThis("Hello World", function callFunction() { console.log("This is a callback function.") });Example: This example shows custom callback in JavaScript. HTML <!DOCTYPE html> <html> <head> <title> How to create a custom callback in JavaScript? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b> How to create a custom callback in JavaScript? </b> <p> See the console for output of the functions </p> <script type="text/javascript"> function processThis(message, callback) { console.log( "Running function first with message: " + message); if (typeof callback == "function") callback(); } processThis("Hello World", function callbackFunction() { console.log("This is a callback function.") }); </script> </body> </html> Output:Non anonymous callback functionA callback function is not always required to be defined as an anonymous function. It may be defined elsewhere and this function can be used later as a callback. The parentheses are not used when passing the callback function. Example: HTML <!DOCTYPE html> <html> <head> <title> How to create a custom callback in JavaScript? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>How to create a custom callback in JavaScript? </b> <p>See the console for output of the functions</p> <script type="text/javascript"> function processThis(message, callback) { console.log("Running function first with message: " + message); if (typeof callback == "function") callback(); } function callbackFunction() { console.log( "Running callback function next"); } processThis("Hello World", callbackFunction); </script> </body> </html> Output:Arguments in a callback functionThe callback function can also have its own arguments and the values can be passed while invoking the callback function in the body of the calling function. Example: HTML <!DOCTYPE html> <html> <head> <title> How to create a custom callback in JavaScript? </title> </head> <body> <h1 style="color: green"> GeeksforGeeks </h1> <b>How to create a custom callback in JavaScript?</b> <p>See the console for output of the functions</p> <script type="text/javascript"> function processThis(message, callback) { console.log( "Running function first with message: " + message); if (typeof callback == "function") callback(9, "Hello!"); } function callbackFunction(num, str) { console.log("Running callback function next"); console.log("num value is: " + num); console.log("str value is: " + str); } processThis("Hello World", callbackFunction); </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Create a Custom Callback in JavaScript? sayantanm19 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Create Customizable Alerts in JavaScript ? Alerts in JavaScript are an important components in web design. They are commonly used to notify users. The SweetAlert2 library aims to make basic looking alerts much more attractive and also provide context to the alert. The documentation and usage examples of SweetAlert2 could be found here. Insta 2 min read How to create custom errors in JavaScript ? In this article, we will learn to create custom errors with some examples. Errors represent the state of being wrong in condition. Javascript handles a predefined set of errors by itself but if you want to create your own error handling mechanism you can do that with custom errors functionality avai 2 min read How to Delay a Function Call in JavaScript ? Delaying a JavaScript function call involves executing a function after a certain amount of time has passed. This is commonly used in scenarios where you want to postpone the execution of a function, such as in animations, event handling, or asynchronous operations. Below are the methods to delay a 2 min read How to Create an Alert in JavaScript ? The alert() method in JavaScript displays an alert box with a message and an OK button. It's used when you want information to come through to the user, providing immediate notifications or prompts for user interaction during program execution. Note: Alert boxes interrupt user interaction, shifting 1 min read How to create an Asynchronous function in Javascript? JavaScript is a single-threaded and synchronous language. The code is executed in order one at a time. But Javascript may appear to be asynchronous in some situations. Example: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title 6 min read How to Convert Callback to Promise in JavaScript ? Asynchronous programming in JavaScript often involves the use of callbacks. However, callbacks can lead to callback hell and make the code harder to read and maintain. Promises provide a cleaner way to handle asynchronous operations. Converting existing callback-based code to use promises can improv 2 min read How to connect to an API in JavaScript ? An API or Application Programming Interface is an intermediary which carries request/response data between the endpoints of a channel. We can visualize an analogy of API to that of a waiter in a restaurant. A typical waiter in a restaurant would welcome you and ask for your order. He/She confirms th 5 min read How to call JavaScript function in HTML ? In HTML, you can easily call JavaScript functions using event attributes like onclick and onload. Just reference the function name within these attributes to trigger it. You can also call functions directly within script blocks using standard JavaScript syntax. Let's create an HTML structure with so 2 min read How to make ajax call from JavaScript ? Making an Ajax call from JavaScript means sending an asynchronous request to a server to fetch or send data without reloading the web page. This allows dynamic content updates, enhancing user experience by making the web application more interactive and responsive.There are multiple ways to make Aja 4 min read How to create a JavaScript callback for knowing if an image is loaded ? The task is to know if an image is loaded using a JavaScript callback method. Please refer to JavaScript callbacks.Method 1: By using .complete property.HTML <!DOCTYPE html> <html> <head> <title> Create a JavaScript callback for knowing if an image is loaded </title> 2 min read Like