Before delay (... waits for 5 seconds) After delay
1) Heap memory: Data stored randomly and memory allocated.
2) Stack memory: Memory allocated in the form of stacks. Mainly used for functions.
Order at which functions get executed i.e get popped out of the stack after a function’s purpose gets over as shown below:
Event loop: An event loop is something that pulls stuff out of the queue and places it onto the function execution stack whenever the function stack becomes empty.
Here the callback function in the event queue has not yet run and is waiting for its time into the stack when the SetTimeOut() is being executed and the Web API is making the mentioned wait. When the function stack becomes empty, the function gets loaded onto the stack as shown below:
That is where the event loop comes into picture, it takes the first event from the Event Queue and places it onto the stack i.e in this case the callback function. From here, this function executes calling other functions inside it, if any.