Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is call stack in JavaScript

Understanding the Call Stack

Let's dive into an important concept in JavaScript - the Call Stack. Don't let the name intimidate you. Think of it like a stack of books. You can only take off the top book to read, and when you're done you put it back. The call stack in JavaScript works similarly. It's a mechanism the JavaScript interpreter uses to handle function calls and remember where it is in the program.

What is a Stack?

Before we jump into the call stack, let's first understand a stack. A stack is a data structure that follows the LIFO (last in, first out) principle. This means that the last item you put into the stack is the first one to come out. Picture it like a stack of pancakes. The last pancake you put on the stack will be the first one you eat!

let stack = [];

stack.push("pancake1");
stack.push("pancake2");
stack.push("pancake3");

console.log(stack.pop()); // Outputs: pancake3
console.log(stack.pop()); // Outputs: pancake2
console.log(stack.pop()); // Outputs: pancake1

The Call Stack in Action

Let's take a look at how JavaScript uses a call stack. When a script calls a function, JavaScript places it on the top of the call stack and starts executing it. As soon as the function finishes its job, it's removed from the stack, and JavaScript continues executing the code that is left.

function firstFunction() {
    secondFunction();
    console.log('Called first function');
}

function secondFunction() {
    thirdFunction();
    console.log('Called second function');
}

function thirdFunction() {
    console.log('Called third function');
}

firstFunction();

If you run the above code, the console will output the following:

Called third function
Called second function
Called first function

This is because when firstFunction() is invoked, it's placed on the call stack. Inside firstFunction(), secondFunction() is invoked and placed on top of the call stack. Then, thirdFunction() is invoked within secondFunction() and, you guessed it, placed on top of the stack.

Once thirdFunction() finishes execution and logs 'Called third function', it's removed from the stack. Then, secondFunction() logs 'Called second function' and is removed from the stack. Finally, firstFunction() logs 'Called first function' and is also removed from the stack.

Call Stack and Recursive Functions

A recursive function is a function that calls itself. When a recursive function is called, it's added to the call stack and removed once it has completed execution. However, if a recursive function keeps calling itself without a termination condition, the call stack will overflow. This is known as a stack overflow.

function recursive() {
    console.log('Calling myself');
    recursive();
}
recursive();

The above code will result in a RangeError: Maximum call stack size exceeded. This error is raised when the call stack has more function calls than it can handle. This often happens with infinite recursive calls.

Conclusion

JavaScript is like a meticulous librarian. Whenever a function is called, it carefully places it on the top of the call stack, and when the function finishes, it neatly removes it. The call stack also serves as a record of where the JavaScript interpreter is in the program, helping it keep track of what function is running and what functions are called inside that function.

So, the next time you're coding in JavaScript, imagine you're that librarian, efficiently stacking and unstacking books, or well, function calls. It's a simple yet powerful concept that's at the heart of JavaScript execution. Happy coding!