What is the Call Stack in JavaScript ?
Last Updated :
10 May, 2025
In JavaScript, the Call Stack is an essential concept that helps the JavaScript engine keep track of function execution. It plays a vital role in managing the execution order of functions and determining how the JavaScript program handles function calls.
How Does the Call Stack Work?
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 called, the JavaScript engine pushes the function onto the Call Stack. The function remains in the stack until it completes its execution.
- Executing the Function: The JavaScript engine processes the function in the stack. It runs the code inside the function until it reaches the end or encounters another function call.
- Nested Function Calls: If a function calls another function, the new function is pushed onto the Call Stack. The engine continues to execute the newly pushed function while the previous one waits for completion.
- Returning from a Function: Once a function finishes executing, it is removed (popped) from the stack. The JavaScript engine then continues executing the function that is now at the top of the stack.
- Completion of Program: The process continues until all functions in the stack are executed and removed. Once the stack is empty, the JavaScript engine has completed the execution of the program.
Now let's understand this with the help of example
JavaScript
function f1() {
console.log('Hi by f1!');
}
function f2() {
f1();
console.log('Hi by f2!');
}
f2();
Output
Hi by f1!
Hi by f2!
In this example: 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.

- Step 2: The f2() function gets called, and the execution context of f2() gets pushed into the stack.

- Step 3: The execution of f2() starts and during its execution, the f1() function gets called inside the f2() function. This causes the execution context of f1() to get pushed in the call stack.

- Step 4: Now the f1() function starts executing. A new stack frame of the console.log() method will be pushed to the stack.

- Step 5: When the console.log() method runs, it will print "Hi by f1" and then it will be popped from the stack. The execution context will go back to the function and now there are no lines of code that remain in the f1() function, and as a result, it will be popped from the call stack.
- Step 6: This will similarly happen with the console.log() method that prints the line "Hi by f2" and then finally the function f2() would finish and would be pushed off the stack.
What is Stack Overflow ?
A stack overflow occurs when the call stack exceeds its memory limit, typically due to excessive function calls. This can happen when a program enters into infinite recursion or if too many functions are called and the stack runs out of space.
JavaScript
function funcA() {
funcB();
}
function funcB() {
funcA();
}
In this example
- funcA calls funcB, which adds funcA to the stack.
- funcB calls funcA, which adds funcB again.
- This cycle continues, eventually exceeding the available stack space
Why Do We Need the Call Stack?
- Tracks Function Execution: Keeps track of which function is running at any given time.
- Handles Nested Calls: Manages nested function calls, ensuring that functions run in the correct order.
- Manages Recursion: Helps with recursive functions by keeping track of each function call.
- Ensures Proper Return: Ensures the program returns to the correct point after each function finishes executing.
- Manages Program Flow: Helps maintain the correct flow of execution, especially in complex programs with many functions.
Conclusion
The Call Stack in JavaScript manages the order of function execution, handling nested calls and recursion. It ensures functions run in the correct sequence and are properly returned to after execution. Improper use, like infinite recursion, can lead to a stack overflow. Understanding it is key to maintaining efficient program flow.
Similar Reads
What is Call in JavaScript ? The call method is used to invoke the function with different this object. In JavaScript, this refers to an object. It depends on how we are calling a particular function. In the global scope, this refers to the global object window. The inside function also refers to the global object window. In st
2 min read
What is Callback Hell in JavaScript ? One of the primary ways to manage asynchronous operations in JavaScript is through callback functions that execute after a certain operation completes. However, excessive use of callbacks can lead to an issue known as Callback Hell, making code difficult to read, maintain, and debug.What is Callback
4 min read
What is the (function() { } )() construct in JavaScript? If you've ever played around with JavaScript, you might have seen this expression. It's like a strange set of symbols, but it has a special name that is an immediately invoked function expression, or IIFE. In this article, we will understand each element of the expression as well as the functionalit
3 min read
Why is Currying in JavaScript Useful ? Currying in JavaScript is a functional programming technique that involves transforming a function that takes multiple arguments into a sequence of functions, each taking a single argument. This technique is particularly useful in JavaScript due to its support for higher-order functions and closures
3 min read
What is JavaScript? JavaScript is a powerful and flexible programming language for the web that is widely used to make websites interactive and dynamic. JavaScript can also able to change or update HTML and CSS dynamically. JavaScript can also run on servers using tools like Node.js, allowing developers to build entire
6 min read
What is the syntax for leading bang! in JavaScript function ? Before we get to know the syntax for the leading bang! in a JavaScript function, let's see what functions in JavaScript are actually are. JavaScript functions are a set of statements(procedures) that perform some tasks or calculate some value. A function may take some input and return the result to
2 min read