In JavaScript, the Call Stack is a core mechanism used by the engine to manage and track function execution.
It keeps track of function calls in the order they are executed.
It helps determine which function is currently running and what runs next.
Working of Call Stack
JavaScript operates in a single-threaded environment, meaning that it can only execute one operation at a time. The Call Stack is the part of JavaScript that keeps track of the execution process.
Function Call: When a function is invoked, it is pushed onto the Call Stack and stays there until execution completes.
Function Execution: The JavaScript engine executes the functionβs code until it ends or calls another function.
Nested Calls: If a function calls another function, the new function is pushed onto the stack while the previous one waits.
Function Return: After execution finishes, the function is popped from the stack and control returns to the previous function.
Program Completion: This continues until the stack is empty, indicating the program has finished executing.
The steps and illustrations below explain the call stack of the above function.
[Step 1]: When the code loads in memory, the global execution context gets pushed in the stack.