Functions are an important part of programming. A normal function simply takes some input, does something using logic and returns us an output. Now this input – means argument passed in a function can be a function itself. This function which is passed into another function as an argument is known as Callback functions.
But we know, Javascript has only one call stack and every function creates an executional context inside the call stack. When a callback function is invoked, it doesn’t create an executional context inside the call stack of the JS engine. Instead, it goes out of the call stack and stays in the JS runtime environment (browser, node.js). When the callback function is ready to be executed, it goes into the callback queue. It waits there until the call stack is empty. Once the call stack is empty, the event loop allows callback functions to go inside the call stack and it gets executed.
Thus anytime a callback function is called, we actually lose the control over it and we don’t know when and in which order it’ll be called as Event loop and callback queue handles that part.
4 Comments
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
1
Thanks for this marvellous post, I am glad I noticed this site on yahoo.
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.