Everything in Java Script is an Execution Context.

Imagine Execution Context as a container. The container has two parts which is memory and code.

Untitled

When the program execution starts, the JavaScript goes through the code and creates memory for all the variables and functions in the code. At the beginning, as none of the variables are initialized, the values of the variables are stored as ‘undefined’. where as for the functions the code is stored as it is.

The data is stored in memory as key value pairs.

Once the memory allocation is completed, the code execution starts where each and every line is executed one at a time in call stack.

When the variable initialization is encountered, then the undefined keyword is replaced with the value of the variable.

If the function invocation is encountered, then the new function Execution Context is created and the above process repeats like memory allocation and code execution.

Once the execution is completed, the execution context is garbage collected and the memory is freed up.