Call Stack Execution
JavaScript engine uses a call stack to manage Execution Context : the Global Execution Context and Function Execution Contexts.
The call stack works based on the LIFO principle i.e., last-in-first-out.
When you execute a script, the JavaScript engine creates a Global Execution Context and pushes it on top of the call stack.Whenever a function is called, the JavaScript engine creates a Function Execution Context for the function, pushes it on top of the Call Stack, and starts executing the function.If a function calls another function, the JavaScript engine creates a new Function Execution Context for the function that is being called and pushes it on top of the call stack.When the current function completes, the JavaScript engine pops it off the call stack and resumes the execution where it left off in the last code listing.
The script will stop when the call stack is empty.
JavaScript call stack example
function add(a, b) {
return a + b;
}
function average(a, b) {
return add(a, b) / 2;
}
let x = average(10, 20);
When the script runs, the JavaScript engine places the global execution context (denoted by main() or global() function on the call stack.
The global execution context enters the creation phase and moves to the execution phase.
The JavaScript engine executes the call to the average(10, 20) function and creates a function execution context for the average() function and pushes it on top of the call stack:
The JavaScript engine starts executing the average() since it is at the top of the call stack.
Get Rahul Kumar’s stories in your inbox
Join Medium for free to get updates from this writer.
The average() function calls add() function. At this point, the JavaScript engine creates another function execution context for the add() function and places it on the top of the call stack:
JavaScript engine executes the add() function and pops it off the call stack:
At this point, the average() function is on top of the call stack, JavaScript engine executes it and pops it off the call stack.
Now, the call stack is empty so the script stops executing:
Stack overflow
The call stack has a fixed size, depending on the implementation of the host environment, either the web browser or Node.js.
If the number of the execution contexts exceeds the size of the stack, a stack overflow will occur.
For example, when you execute a recursive function that has no exit condition, it will result in a stack overflow error:
function foo() {
foo();
}foo(); // stack overflow
Asynchronous JavaScript
JavaScript is the single-threaded programming language. The JavaScript engine has only one call stack so that it only can do one thing at a time.
When executing a script, the JavaScript engine executes code from top to bottom, line by line. In other words, it is synchronous.
Asynchronous is the opposite of synchronous, which means happening at the same time. So how does JavaScript carry asynchronous tasks such as callbacks, promises, and async/await? This is where the event loop comes into the picture that we will cover in the next tutorial.
In this tutorial, you have learned about the JavaScript call stack that helps keep track of the execution contexts or function calls.








