Callback Functions in Java Script
JavaScript is a language that handles multiple tasks simultaneously. This implies that JavaScript is an asynchronous programming language. Callbacks are a type of function that are a fundamental aspect of JavaScript. In this blog, we’ll look at what callbacks are, why they’re used, and how to use them with examples.
What are Callback Functions?
- A callback is a function that is passed as an argument to another function.
- A callback allows you to run code after an asynchronous operation has been completed.
- Callbacks are called after the main function has finished its execution.
- The function in which callback is passed as a parameter is called a main function.
Why are Callback functions used?
- Callbacks allow you to continue executing code while the operation is being executed in the background.
- When the main function finishes it calls the callback function to provide us with the obtained results.
- Callback in this way helps you to ensure that the program remains responsive and the user experience is not impacted.
Example of a callback function:
Output or the above callback function is :