| 1. |
JavaScript Memory Allocation and Event Loop |
|
Answer» In JavaScript, memory allocation is done in the following regions:
The function stack is a function that maintains track of all other functions that are running at the same time. An example to illustrate it is as follows: function second() {console.log("Second") } function First() { second() } function foo() { first() } foo() The order in which functions are executed, that is. when they are popped out of the stack once their purpose is completed, is as follows:
The callback function in the event queue has not yet started and is waiting for its time to be added to the stack when SetTimeOut() is called and the Web API waits. The function is loaded onto the stack when the function stack becomes empty, as seen below: The event loop is used to take the first event from the Event Queue and place it on the stack, which in this case is the callback function. If this function is called from here, it will call other functions within it. |
|