A Call Stack is a data structure used by the JavaScript engine to keep track of function invocations. It follows the LIFO (Last In, First Out) principle. Each time a function is called, it's added (pushed) to the stack. When the function returns, it's removed (popped) from the stack.
function sayHi() {
// Function definitions
const a = 10;
const b = 20;
add(5, 7);
}
function add(x, y) {
kuchBhi();
return x + y;
}
function kuchBhi() {
console.log("Kuch Bhi");
}
// Call the function
sayHi();
function introduceMe() {
console.log("Introduce Me.");
introduceMe(); // Infinite Recursion
}
introduceMe(); // Causes Maximum Call Stack Size Exceeded
Open your browser's Console (F12) to view the call stack output when you run the examples.