Execution Context and Call Stack

Search for a command to run...

You're welcome π
Thanks, your article was very helpful, so I had to mention it and it was an eye opener too to me.
Thanks Obi
I'm sharing what I've learnt from a course I took: "JavaScript: The Hard Parts v2" from [Frontend Masters](https://frontendmasters.com/courses/javascript-hard-parts-v2/) by Will Sentance.
Hello! This article is a follow up with my series JavaScript: The Hard Parts v2 Cover from a course I took from Frontend Masters By Will Sentance. Before we move into Callbacks and Higher-Order Functions, let's know what a Function is all about firs...
With the rise of AI agents, vibe coding, and the advancement of software development tools, it's never been easier to build applications quickly. However, these advancements come at a cost to software

Personal highlights along with Work highlights in 2024

π‘ Originally posted here. In the intricate world of web design today, typography serves as a powerful means of expression, improving site usability and it goes beyond the realms of font choice and sizes. In this article weβll explore a sub-section...

Ever spent hours wrestling with images that refuse to stay put in your web layouts? You're not alone. In this comprehensive guide, we will unravel the mystery behind different image centering methods, from the basic and widely used to the more advanc...

Tailwind CSS recently launched some awesome updates in v3.4, supporting CSS features like Dynamic viewport units, subgrid supports and other features, which we'll go over in this article. It's so easy to be carried away by using the already-known fea...

Hello once again! This article is a more like a continuation from my first blog post, so I'll suggest you check it out first.
Also, this article is written from the knowledge I've got from Javascript: The Hard Parts v2 and by also making researches and reading other blogs.
Before diving into the main discussion for this article, let's take a look at Function.
What's a Function?
Simply, a Function is a block of code stored in memory that contain(s) a / several instruction(s) and then returns a value or not, to be used later when invoked.
Functions make code less repetitive.
E.g.
function addingBy2(num) {
return num + 2;
}
Note: The function above has to return a value, not all functions returns a value.
Functions are meant to be called / invoked / run, in order for the function to be executed.
From my previous article, I mentioned, an Execution Context is created when a function is called / invoked.
Execution Context is simply the environment within which code is ran.
The Execution Context has two parts to it, which are :
Thread of Execution and Memory has been discussed here.
Execution Context are of 2 types, which are:
This is created immediately Javascript reads a / series of code.
For example :
var name = "Jome Favourite"
function log() {
console.log("hello")
}
var fruits = ["apple","orange"]
From the instructions above, a Global Execution Context is created and name, log (function name), fruits with their respective values are stored in the Global Memory.
E.g.
name: "Jome Favourite
log: function
fruits: ["apple","orange"]
This is created when ever a function is called / invoked.
Let's look at this example :
function simpleCalc(num) {
let firstNumber = 2
let secondNumber = 3
return firstNumber + secondNumber + num
}
simpleCalc(5)
From the above example, Javascript goes through the code Thread of Execution and sees a function definition on line 1 and then stores the function definition into the Global Memory of the Global Execution Context.
E.g.
simpleCalc: function
Javascript doesn't read what's in a function until the function has been called.
On line 7 where the function simpleCalc has been invoked, Javascript checks the Global Memory if such a function exists.
If the function simpleCalc is found in the Global Memory, then Javascript goes to the function body to execute the instructions provided.
In this case,
simpleCalcdoes exist in the Global Memory containing a function.
Since the function simpleCalc exist, a Function Execution Context is created for it.

An illustration of the Function Execution Context
Let's go through what's happening in the function
To the browser's console, the returned value is shown as 10.
Then the Function Execution Context is deleted / cancelled from the Call Stack since the function is done executing.
The term Call Stack keeps track of where a function is invoked and then holds (a better term will be push(s)) the function until the execution is done and then pop(s) it out from the stack.
Call stack follows "First In Last Out" principle, which simply means what ever function is invoked first, is held on the call stack and there could be order functions invoked from the initial function.
All other functions invoked must be done executing and popped out from the Call Stack, before the initial function is popped out.
For example :
function outer() {
console.log("I'm first")
function inner() {
console.log("I'm second")
function innerinner() {
console.log("I'm third")
}
innerinner()
}
inner()
}
outer()
// Output :
// "I'm first"
// "I'm second"
// "I'm third"
This type of Function definition is known as Nested Functions.
Wow, that's a lot of functions, allow me break it down.
In the Global Execution Context which runs immediately, function outer is stored in the Global Memory.
For example:
outer: function
Then on line 13, the function outer is invoked, which then creates a brand new Function Execution Context and is added to the Call Stack.


Allow me try to break down the above illustrations.
If you feel the picture isn't clear enough, do comment below.
From theCall Stackillustration aboveglobal()is beneath the Call Stack representing the Global Execution Context which is executed first.
Firstly, inside the outer function is console.log("I'm first") which is one of the browser's features known as WEB API, which means Javascript doesn't really have console.log in its language.
inner function is defined, and stored in the outer's function execution Local Memory.inner function is invoked.In this case, Javascript checks if such a function has been defined and stored in memory.
Checks theLocal Memoryfirst on where it has been defined, if it wasn't at the Local Memory of being invoked, then it checks the Global Memory.
Yesinner functionhas indeed been defined and stored.
Since inner function is invoked a new Function Execution Context is created and its then added to the Call Stack.
inner function we have console.log("I'm second").innerinner function is defined and stored in the Local Memory of inner function.On line 8, innerinner function is invoked and another Function Execution Context is created and then added to the Call Stack.
console.log("I'm third") which is held by the browser.Now that all functions invoked is done executing, the innerinner function is popped off the Call Stack and its Execution context is deleted, follow by inner function then lastly outer function.
But the browser know it was to display a value to the console, so it displays the initial console.log which was "I'm first", "I'm second" and "I'm third".
Wow! That's a lot to take in, I know right! π. But the best or worst part, is that there still other concept that I didn't dive into. So, If you'll like to read more on Execution Context and the likes treated in this article, check out this blog by @skay.
Thanks for reading, this is a long one.
Contact me: @twitter