- a callback function is a function
- that is passed as an argument
- to another function and is intended to be called later in the execution of that function.
The purpose of a callback function is to
- allow for asynchronous programming.
- where a function can be executed while other code is running,
- and then execute the callback function when it's finished.
Callback functions are commonly used in event handling,
AJAX requests, and other asynchronous operations.
Q. Interview Question related to Callback functions
Q1.Explain what a callback function is and provide a simple example
A callback function is a function that is passed to another function as an argument and is executed after some operation has been completed.
Below is an example of a simple callback function that logs to the console after some operations have been completed.
`function modifyArray(arr, callback) {
// do something to arr here
arr.push(100);
// then execute the callback function that was passed
callback();
}
var arr = [1, 2, 3, 4, 5];
modifyArray(arr, function() {
console.log("array has been modified", arr);
});`
Google JavaScript Technical Interview (Callbacks, Promises, Await/Async)
Callbacks
What is a Callback in JavaScript
Callbacks in real LifeCallbacks vs. ES6 Promises & Await / Async
Why Promises
What are Promises
How Promises resolves Callback Hell
Why do we even need Callback in JavaScript?
(https://medium.com/developers-tomorrow/google-javascript-technical-interview-7a20accd6ddf)
Top comments (0)