The Call Stack

JavaScript uses a data structure, which we call the call stack to keep track of every function call that’s made. Every function gets added to the call stack and it’s addressed in the order of last in first out, or LIFO. The last function to be executed gets priority to finish execution before falling back to the one that called it. Just imagine a stack of plates, you put plates on top and take plates off the top — this is essentially how the call stack works.

When something goes wrong in your program you’ll get a dump of the call stack in the console to see exactly what route your program took to get to its unexpected end. Maybe your code errored out, but maybe you caused a stack overflow.

A stack overflow is when you pile up the plates without taking any off. Eventually the stack will crash when there are too many plates!

Javascript similarly has a call stack size limit, and when you overflow that limit maybe due to inefficient code or limited memory, it will crash, experiencing a stack overflow.

But, in the event everything goes well, each function will execute last to first as each kicks off and your program will complete! But that’s very single threaded, with each function running one at a time in order from last to first… what if i want to do several things without having to wait?

The answer is Web APIs!


Posted

in

,

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *