JS is one of the 3 core technologies in web development.
HTML :: Content.
CSS :: Presentation.
JS :: Dynamic effects/programming logic.
Execution Context and Execution Stacks
We can think of execution contexts like a box or container which stores variables, and in which a piece of code is evaluated and executed.
The default execution context is always the global context. Global context is for variables and functions which are not inside any function.
To be precise, we can think of global context as an object. The variables will be added as property to the window object.
This object has 3 properties.
- Variable Object.
- Scope Chain.
- “this” variable.
Each time we call a function, it gets its own brand new execution context and sits at top of the global execution context forming the execution stack and this happens in two phases.
Phases of Execution Context
Each execution context has creation and execution phases.
Creation of Variable Object
- An argument object is created containing all the arguments passed into the function.
- Code is scanned for function declarations. For each functions a property is created in the variable object, pointing to the function.
- Code is scanned for variable declarations and a property is created in the variable object set to undefined.
Creation of Scope Chain
- Scope means, where can we access a certain function or variable.
- Each new function creates a new scope.
- Lexical scoping: A function that is lexically within another function gets access to the scope of the outer function. With that it means it gets access to the variables and functions that the parent function defines.
Creation of “this” variable
- “this” points to the object that is calling the function.
The code of the function that generated the current execution context is ran line by line and all the variables are defined.
See you soon!! Happy reading!!