The call stack

We learned about the stack data structure in Chapter 4, Stacks. We will see it in action in a real application using recursion. Whenever a function is called by an algorithm, the function goes on the top of the call stack. When using recursion, each function call will be stacked on top of each other, due to the possibility of one call depending on the result of the previous invocation itself.

We can see the Call Stack in action using the browser, as demonstrated in the following screenshot:

If we execute factorial(3), open the developer tools of the browser, go to Sources, and add a breaking point in the Factorial.js file, when ...

Get Learning JavaScript Data Structures and Algorithms - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.