JavaScript Call Stacks: An Introduction

If you’ve always wondered how a stack actually works but been too afraid to ask, you’ve come to the right place. Let’s dig in.

Written by Julia Zolotarev
Published on Jun. 06, 2022
Brand Studio Logo

I happen to be intimately familiar with stacks...of pancakes — short stacks, tall stacks, all stacks. So pancakes are naturally the first thing that come to mind when I hear “stack.” Luckily, it happens to be a fitting visual metaphor for a software stack.

What Is a Stack?

Software stacks are a data type similar to an array that behaves as a collection of elements. In this visual example, each pancake represents some element, which we’ve added to the top of the stack; we can only remove each element from the top. Because of this linear structure, stacks are also referred to as LIFO  or last in, first out.

Stacks have two main operations: push and pop. If you’re familiar with arrays, you’ll be happy to know that these methods are exactly what you think: Push adds an element to the top of the stack, and pop removes the most recently added element from the top. Unlike an array (or a stack of pancakes), it’s not possible to access an element from the bottom or middle without first removing those on top of it.

What Is a JS Call Stack?

A JavaScript call stack is an example of a stack data structure. In a JS call stack, the elements are function invocations. Every time you invoke a function, it’s added to the stack. If it has a nested function, that nested function will get added to the top of the stack as well, and so on. You can think of the call stack as a to-do list of sorts for JavaScript.

More Expert Explainers From JuliaCreate React App and TypeScript — A Quick How-To

 

Stacks vs. JS Call Stack

A JavaScript call stack is an example of a stack data structure. In a JS call stack, the elements are function invocations. Every time you invoke a function, it’s added to the stack. If it has a nested function, that nested function will get added to the top of the stack as well, and so on. You can think of the call stack as a to-do list of sorts for JavasScript.

It’s important to remember that JavaScript is largely a single-threaded process, meaning that JavaScript processes one command at a time. Asynchronous actions are handled differently and I highly recommend this video if you’d like to learn more about that.

Once you’ve pushed all functions that need to run in a given script to the top of the stack in order of invocation, JavaScript starts resolving them in order from top to bottom, popping them off the stack.

In this video, you can see that the function printSquare is invoked at the bottom of the screen. The printSquare function is added to the call stack, but it can’t be resolved until it receives the return of its nested function called square. The square function is now added to the call stack, but it can’t be resolved until it receives the return of its nested function called multiply. The multiply function gets pushed onto the top of the stack, and since it has a return statement that can be returned right away, it goes ahead and completes and gets popped off the top of the stack. The call stack continues in this manner, popping functions off in reverse of the order in which they were called.

Researching for this article was a bit like managing my own call stack. As I started one line of research, it inevitably brought up a nested question, which I needed to add to the top of my own “to-do” list to understand before continuing; during the research of that second question, I came upon another topic I added to the top of my list before continuing with the previous subject, and so on, multiple times over. Resolving the most recent question I added allowed me to remove it from the top of the list, and continue with the next question underneath until I find myself back at level one, ready to write this blog!

Get More From Built In Software DevsWhat Is the @ Symbol in Python and How Do I Use It?

 

How to Practice With Stacks

The Chrome browser has a really robust dev tools panel. One of the things you can do with the panel is step through code line by line and see it getting pushed onto the stack. To open it, right click (or type Cmd + option + j) on any web page in Chrome, choose the Inspect option, and the Sources tab on the top. I made a small video with a stack call demonstration using the breakpoint and step through tools. 

A gif of functions being added to the call stack and then popped off one by one

I add the breakpoint to the aDayInTheLife function invocation, which pauses the code and allows us to step through each cascading function. As you can see, the functions are added to the stack from bottom to top, then are resolved from top to bottom as the functions receive their return values and are resolved. Ultimately, the string “Made the bus in seconds flat” is passed all the way back to the original aDayInTheLife function and returned in the console at the very end once the stack has been cleared.

The JavaScript Call Stack, Explained

 

What Is Stack Overflow?

The basic premise is that computer memory is finite, and if there are more elements pushed onto the stack than there is space, the stack will overflow. The most common cause of a stack overflow situation is infinite or very deep recursion. A recursive function is one that calls itself — or imagine two functions that are calling each other. The first function adds the second function to the call stack, the second function adds the first function to the call stack and so on. Since nothing is ever returned or resolved, the stack will overflow.

javascript-call-stack

Two functions calling each other will lead to a stack overflow.

javascript-call-stack

As you can see, the Chrome dev tool has error handling for this and will error out after 16,000 frames (call stack elements).

javascript-call-stack

The Beatles saying hello and goodbye forever — no error handling needed.

boomunderground waving beatles GIF
I’ll leave you with a fun fact: Stack Overflow , the website ,  got its name from a 2008 Coding Horror poll deployed by the creators who were looking for help naming their website. Some of the other options were humbledeveloper.com and writeoncereadmany.com. 25 percent of nearly 7,000 votes went to stackoverflow.com, making it the winner, and I, for one, think they chose the best one.

So...who’s hungry? 

Explore Job Matches.