Table of contents
Definition
A call stack refers to the use of the stack data structure to store the execution order of any piece of code.
Use Cases and Examples
A call stack as mentioned above is used to describe the execution order of any piece of code. Most of us read written content from top to bottom. For the most part, software is executed by computers the same way, but sometimes some instructions although read is executed later.
01: var name = 'John Doe';
02:
03: hello();
04:
05: function hello(name) {
06: return name;
07: }
The Javascript code above shows the use of the hello
function on line 03
before its definition on line 05 to 07
, this code will still execute fine. The reason is that when the interpreter [→] first scans through the code it reorders everything and places them in a call stack through a process known as hoisting [↗], then execute the code based on the order found in the call stack.
Summary
A stack data structure naturally involves accessing data from top to bottom which is why it's perfect for storing the execution order of a program.
Here is another article you might like 😊 What Is A Callback?