JavaScript call stack
Last updated
Was this helpful?
Last updated
Was this helpful?
The JavaScript engine (which is found in a hosting environment like the browser), is a single-threaded interpreter comprising of a heap and a single . The browser provides web like the , , and Timers.
An understanding of the call stack will give clarity to how βfunction hierarchy and execution orderβ works in the JavaScript engine.
The call stack is primarily used for invocation (call). Since the call stack is single, function(s) execution, is done, one at a time, from top to bottom. It means the call stack is synchronous.
The understanding of the call stack is vital to programming (which we will look at later).
In Asynchronous JavaScript, we have a , an event loop, and a task queue. The callback function is acted upon by the call stack during execution after the call back function has been pushed to the stack by the event loop.
But before we jump the gun, let us first attempt to answer the question - What is the call stack?
At the most basic level, a call stack is a data structure that uses the Last In, First Out (LIFO) principle to temporarily store and manage function invocation (call).
Letβs break down our definition:
LIFO: When we say that the call stack, operates by the data structure principle of Last In, First Out, it means that the last function that gets pushed into the stack is the first to be pop out, when the function returns.
Let us take a look at a code sample to demonstrate LIFO by printing a stack trace error to the console.
When the code is run, we get an error. A stack is printed showing how the functions are stack on top each other. Take a look at the diagram. (Stack trace error)
You will notice that the arrangement of the functions as a stack begins with the firstFunction()
(which is the last function that got into the stack, and is popped out to throw the error), followed by the secondFunction()
, and then the thirdFunction()
(which is the first function that gets pushed into the stack when the code is executed).
Temporarily store: When a function is invoked (called), the function, its parameters, and variables are pushed into the call stack to form a stack frame. This stack frame is a memory location in the stack. The memory is cleared when the function returns as it is pop out of the stack.
Manage function invocation (call): The call stack maintains a record of the position of each stack frame. It knows the next function to be executed (and will remove it after execution). This is what makes code execution in JavaScript synchronous.
Think of yourself standing on a queue, in a grocery store cash point. You can only be attended to after the person in front of you have been attended to. Thatβs synchronous.
This is what we mean by βmanage function invocationβ.
We will answer this question by looking at a sample code of a function that calls another function. Here is the example code: (Here is the output)
This is what happens when the code is run:
1. When secondFunction()
gets executed, an empty stack frame is created. It is the main (anonymous) entry point of the program.
2. secondFunction()
then calls firstFunction()
which is pushed into the stack.
3. firstFunction()
returns and prints βHello from firstFunctionβ to the console.
4. firstFunction()
is pop off the stack.
5. The execution order then move to secondFunction()
.
6. secondFunction()
returns and print βThe end from secondFunctionβ to the console.
7. secondFunction()
is pop off the stack, clearing the memory.
A stack overflow occurs when there is a recursive function (a function that calls itself) without an exit point. The browser (hosting environment) has a maximum stack call that it can accomodate before throwing a stack error.
Here is an example:
The callMyself()
will run until the browser throws a βMaximum call size exceededβ. And that is a stack overflow. (Maximum call stack error)
The key takeaways from the call stack are: 1. It is single-threaded. Meaning it can only do one thing at a time. 2. Code execution is synchronous. 3. A function invocation creates a stack frame that occupies a temporary memory. 4. It works as a LIFO β Last In, First Out data structure.