# How JavaScript Works(part 3):

## Handling Execution Context (Call Stack)
It is very difficult for JS to manage execution context but it manages it very beautifully. It manages the execution context creation/deletion, and control through **call stack**.

**Note:**
If you don't know what execution context is, I really suggest you to read my previous blogs about it.

[How JavaScript Works(Part 1):](https://itsismatullah.hashnode.dev/how-javascript-workspart-1)

[How JavaScript Works(Part 2)](https://itsismatullah.hashnode.dev/how-javascript-workspart-2)
 

Now back to our topic:

The call stack is sometimes known as runtime stack, control stack, and execution context stack.
**So call stack maintains the order of execution of the execution context.**

Let's see how this is done through the diagram.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658216267392/tUvJYt3DQ.png align="left")
Whenever a JS program is run its call stack is populated with Global Execution Context **(GEC)**. Every time at the bottom of the call stack, we have GEC. 

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658216785853/gd3llq56X.png align="left")

Now whenever a new function is invoked or a new execution context is created, that execution context is pushed into the stack.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658216549638/umDcQX0TD.png align="left")
After the work of this execution context is done, this execution context from the call stack is popped out and control goes back to the Global execution context.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658216730910/fgb0pC2sx.png align="left")

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658216766618/LJa0NzaEl.png align="left")

After the whole program is done, the Global execution context also pops out from call stack and the call stack again becomes empty.

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1658216911417/u59M0-44O.png align="left")

