To see the behind the scene workings of asynchronous code getting executed in the browser, let us first see an example of asynchronous code.
Once the execution is finished the Global Execution Context gets removed from the Call Stack. Now, after the timer gets expired, to print the greeting message we need to somehow fetch the callback function greet to the Call Stack in order to execute. This is done by Event Loop and Callback Queue.
After the timer gets expired, the callback function is put inside the Callback Queue and the job of the Event Loop is to check if the Call Stack is empty and if empty, push the callback function from Callback Queue to Call Stack and the callback function gets removed from the Callback Queue. Then the Call Stack creates an Execution Context of the call back function and executes it.
Therefore, the output of the code will be:
Hi! Welcome to GeeksforGeeks!