How does code get executed in JavaScript?
Categories: Development
JavaScript is a coordinated (Moves to the following line just when the execution of the ongoing line is finished) and single-strung (Executes each order in turn in a particular request in a steady progression sequentially) language. To be aware behind the location of how JavaScript code gets executed inside, we need to realize something many refer to as Execution Context and its job in the execution of JavaScript code.
Execution Context: Everything in JavaScript is wrapped inside Execution Context, which is a theoretical idea (can be treated as a compartment) that holds the entire data about the climate inside which the ongoing JavaScript code is being executed.
Presently, an Execution Context has two parts and JavaScript code gets executed in two stages.
Memory Allocation Phase: In this stage, every one of the capabilities and factors of the JavaScript code get put away as a key-esteem pair inside the memory part of the execution setting. On account of a capability, JavaScript duplicated the entire capability into the memory block yet on account of factors, it doles out unclear as a placeholder.
Code Execution Phase: In this stage, the JavaScript code is executed each line in turn inside the Code Component (otherwise called the Thread of execution) of Execution Context.
In the above JavaScript code, there are two factors named number and newNumber and one capability named Square which is returning the square of the number. So when we run this program, Global Execution Context is made.
In the Code Execution Phase, JavaScript being a solitary string language again goes through the code line by line and updates the upsides of capability and factors which are put away in the Memory Allocation Phase in the Memory Component.
So in the code execution stage, at whatever point another capability is called, another Execution Context is made. In this way, every time a capability is conjured in the Code Component, another Execution Context is made inside the past worldwide execution setting.
As may be obvious, the qualities are doled out in the memory part subsequent to executing the code line by line, for example number: 2, res: 4, newNumber: 4.
After the return proclamation of the conjured capability, the returned esteem is alloted instead of unclear in the memory portion of the past execution setting. Subsequent to returning the worth, the new execution setting (transitory) gets totally erased. Whenever the execution experiences the return explanation, It gives the control back to the execution setting where the capability was conjured.
Subsequent to executing the principal capability call when we call the capability once more, JavaScript makes again another impermanent setting where a similar strategy rehashes likewise (memory execution and code execution). Eventually, the worldwide execution setting gets erased very much like youngster execution settings. The entire execution setting for the example of that capability will be erased
Call Stack: When a program begins execution JavaScript drives the entire program as worldwide setting into a stack which is known as Call Stack and proceeds with execution. At the point when JavaScript executes another specific circumstance and simply follows similar cycle and pushes to the stack. At the point when the setting gets done, JavaScript simply pops the highest point of the stack likewise.