- In this the code gets evaluated line by line, variables & objects etc get stored in the Memory heap that are then used during the execution of that piece of code thus forming an environment that enables execution of the JS code.
- It follows a LIFO (Last-In, First-Out) principle, meaning that the last function called gets to the top of the call stack and once execution is finished, it pops from the stack.
Credits- Babs Craig
- It means that when a script is run, the JS engine runs the code line by line, beginning at the top and working its way down.
windowobject and in Node.js we can find the same in the form of the
- Every function, once called for execution, generates an execution context that then gets pushed to the top of the call stack thus getting in line to get the access to all the resources (variables, functions, objects) required for its execution.
- After the execution of all the functions in the code is complete, the Global Execution context is also popped off the call-stack. Credits- Danny Zhang
1) Global Execution Context-
- It is the base or the default execution context. Any code that is not present inside any function is said to be inside the Global Execution context that is why we are able to access them using the
- It also provides us with the
thiskeyword to help reference the
- Create a memory heap in order to store variables and function references.
- Stores all the function declarations in the memory heap and the variables in the Global Execution context with its initial values as
undefined. Credits- Lexical
2) Functional Execution Context-
- These are created for every function call thus, unlike Global Contexts, we can have multiple Functional Execution contexts.
- They can access all the code of the Global Context but the Global Context cannot access the code of the Functional Execution context.
3) Eval Execution Context-
- Any code executed via the
evalfunction has its own executional context.
1) Creation Phase-
Task 1- Creation of Activation/ Variable object
- The activation object is like a memory unit/ container for storing the variables, objects etc related to a function.
Task 2- Creation of Scope Chain
- Scope chain is the list of variables and objects created within a particular function.
- Once the scope chain is formed, it helps initialize
2) Execution Phase-
- The JS engines scan over the function in the code one more time, updating the variable object with the values of the variables and then running the code.
- Stack overflow occurs when the call stack becomes full/ can fit no more function calls or contexts.
- This may occur when a recursive function is run with no exit point and the function exceeds the storage limit of the call stack.
Storage of the call stack is dependent on the host environment, the web browser or the Node.js env.
Open Code in JS Fiddle
Watch this amazing illustration on this topic to grasp the topic even better. We will understand the callback queue in this illustration in the future parts of the series.