- Why do you need to know the execution context in JS?
- What is the execution context?
- Where and how it is used?
- This will help you understand other concepts like hoisting, scope chain, closure, memory allocation, design pattern and framework.
What is the execution context?
Execution context has 2 phases
- Creation Phase
- Code execution Phase
A. Creation Phase | Execution Context
During this phase,
- Scope chain (outer environment) is created.
- The global object is created and ‘this’ is set in during this phase.
B. Code execution phase | Execution Context
In this phase,
- the assignment to variables is done.
- the code is executed line by line.
Where and how it is used?
- str is allocated memory and assigned to undefined.
- function greet() is dumped as it is in memory.
- this refers/points to the window object (more reference in coming tutorial)
Code execution Phase:
- Line 3 is executed and greet() function is called as it available in memory and “Hello World!” gets printed in console.
This concept will help you understand other concepts like hoisting, scope, and closures more easily and deeply.