Basics of JavaScript

Akshaya Gupta
3 min readMay 16, 2021

How does Javascript work and what is the Execution context?

Everything inside javascript happens inside the Execution context. The execution context has two parts (i)memory component (also known as Variable environment ) and (ii)code component (also known as Thread of execution).

The memory component stores a key-value pair. For example Key: a Value:10.

In the code component, code is executed one line at a time.

Javascript is a Synchronous single-threaded language. It means it can execute one command at a time in a specific order.

How JavaScript Code is executed and what is Call Stack?

The execution context is created in two phases memory creation phase and the code execution phase.

In the first phase, it will reserve or allocate memory for variable x and function add. It will store an undefined value for x and in the case of a function, it literally stores the whole code inside the memory space. It also stores undefined for two other variables.

In the second phase aka the code execution phase, the value 3 on line number 2 will be placed inside x which also called a place holder or identifier. From line 4 till line 7 there is nothing to execute. So it will move to line number 9 and it invokes the function additionOddNumber(3,5). This function will create a new local execution context inside the global execution context.

In the first phase of the local execution context, it will reserve memory for the variables inside the function.

In the second phase of the local execution context aka the code execution phase. The value 3 of x will pass to num1, value 5 will pass to num2 and num1+num1 will be executed in the code block which will replace the undefined value of the sum. After that, it will call return which will take the value of sum and update additionOddNumber and give control to additionalOddNumber.

Once this is done then the local execution context for the function additionOddNumber will be deleted. Now it will go to the next line number 10 and again do the same.

Now we do not have anything to execute it will also delete the local execution context of additionEvenNumber.

All these things managed inside the call stack with every single line of code execution. It maintains the order of execution contexts.

--

--