Exploring JavaScript Execution Context: Key Fundamentals and Functionality
How JavaScript works and execution context
Core fundamentals:
Everything in JavaScript happens inside the execution context.
The execution context is like a container in which the entire JavaScript code is executed.
Components of execution context:
Execution context has two main components
Memory component (variable component):
This component stores variables and functions as key-value pairs.
Variables like 'a' with Value 10 are stored in the memory component.
Functions are also stored in the memory component.
Code component (thread of execution):
The code component is where the code is executed line by line.
It is known as the thread of execution.
JavaScript is single-threaded, meaning it can only execute one command at a time.
JavaScript is also asynchronous, which means it executes commands in a specific order, waiting for the current command to finish before moving to the next one.
How JavaScript is executed:
var n =2;
function square(num){
var ans = num*num;
return ans;
}
var Square2 = Square(n);
var Square4 = Square(4);
Memory creation phase:
In the memory creation phase, JavaScript allocates memory to all variables and functions in the program.
Variables are assigned a special value known as "undefined" during this phase.
Functions, on the other hand, have their entire code stored in the memory space allocated to them.
Consider the above code and figure. If the code contains a variable 'n' with the value of 2 and a function 'square' that returns the square of a number, memory is allocated to 'n,' storing the value "undefined," and 'square,' storing its entire function code.
Any other variables are also allocated in memory with the initial value "undefined."
Code execution phase:
In the code execution phase, JavaScript goes through the program line by line and executes the code.
When it encounters a line that assigns a value to a variable, it places that value inside the corresponding memory location.
For example, if 'n' was initially assigned "undefined" during code execution, it would now be assigned the value of 2.
When a function is invoked (called), a new execution context is created for that function.
This new execution context also has two components: memory and code.
The function's parameters and local variables are also allocated memory with the initial value "undefined."
The code inside the function is executed, and calculations are performed.
When the function is done executing, its execution context is deleted.
Call stack:
Whenever an execution context is created, it is pushed onto the call stack.
The call stack manages the order of execution for the execution context.
When a function is invoked, a new execution context is created and pushed onto the stack.
When the function is done executing, its execution context is popped out of the stack, and the control returns to the previous execution context.
The call stack helps maintain the flow of control during the program execution.
If a function invokes another function, multiple execution contexts can be stacked on top of each other, forming a chain.
The call stack can handle multiple levels of function invocations, managing the order in which they are executed.