definition and Type of execution context | code execution , callstack and hoisting in Javascript | Interview manthan
Top 3 JavaScritp Interview Question
Execution context :
It is an environment where javascript code is executed. Here environment is referring to this, variable,objects,function etc is accessiable at perticular time.
Example : It could be like a container where everything a task that we perform by javascript will take place.
Type of execution context:
There are two two types in execution context.
Globle execution context : It is globle context where every code and its referrence is present and there is only one globle executionl context in javascript. when page is load it is the first thing to load on callstack(defined below).
Functional/local execution context : It is local context of a code where it's code is accessible.Code inside the function will avaible for itself and child funtion/local scope.
There are two phase of execution context:
When page is load js engine will not perform task directly on the variables,objects etc. The process is dived in two phases.
Creation phase: In this phase three works are done.
- Creating Activation object / Variable Object : This object will consist of all variable and function name but without initialization of the respected value. For variable it will initialise them with undefined keyword and function expression is stored in saperate heap space where respected function will also initialised with undefined keyword. If function is not function expression and defined normally so function name will directly point to the function definition in heap.
- Scope chain : it will keep history of current and all parent function's variable objects.
- this keyword : it also initialised value of this.
Code Execution phase: In this phase all respected value is initialised to respected variable and function.
CallStack :
Callstack load execution context when needed. Globle execution context is stored in it by default and after the when a function is invoke that function push into the callstack and after completion of task that function pop out from the stack. Callstack work as stack data structure "Last in First out".
Hoisting:
It is process of accessing data even before declared.
a = 5;
console.log(a);
var a;
in the above code a is initialsed with value 5 and is declared after the console.log(). and we accessing value of a even before initailsing it. this is called as Hoisting and this could be possible because of scope chain.
We can Hoist function as well.
run();
function run(){
console.log("Running...............");
}
Comments
Post a Comment