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 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 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 Call a JavaScript Function from Chrome Console ? One can call a JavaScript Function from the Chrome Console. We will learn how can we call the function in the console that is written in JavaScript. Steps to call a JavaScript Function from Chrome ConsoleOpen the Chrome ConsoleYou can open the Chrome Console by right-clicking on your webpage, select 2 min read Understanding Callbacks and Callback Hell in JavaScript In JavaScript, callbacks are used for handling operations like reading files and making API requests. When there is excessive nesting of the functions it leads to a problem known as the callback hell. Due to this, it becomes difficult to read the code, debug, and maintain. But when we implement the 5 min read What is Callback Hell in JavaScript ? One of the primary ways to manage asynchronous operations in JavaScript is through callback functions that execute after a certain operation completes. However, excessive use of callbacks can lead to an issue known as Callback Hell, making code difficult to read, maintain, and debug.What is Callback 4 min read How to Delay a JavaScript Function Call using JavaScript ? Delaying a JavaScript function call involves postponing its execution for a specified time using methods like setTimeout(). This technique is useful for timed actions, animations, or asynchronous tasks, enabling smoother user experiences and better control over when certain operations run.There are 3 min read Like