The context (execution context) in JavaScript?

The flow of execution of program is known as execution context.

Global code –Initial section of program execution is known as global code.
Function code – Once the execution enters into a function call.
<script>
var a=”Global execution”;  // Global context
function myfun(){
var b=”Global execution”;
}
myfun(); // Functional context
</script>

Execution Context Stack

JavaScript interpreter in a browser is known as Execution Context Stack.

Each function call creates a new execution context.

So once a new function called , that will be pushed to the top of execution stack(on the top) and once its completed it will be pop out.

2 stages are there for “execution context”:
->Creation Stage
Create the Scope Chain.
Create variables, functions and arguments.
Determine the value of “this”.

In creation stage or stage 1 , executioncontextobject is created. This is created before executing the function. in this stage, determain the variables,parameters, function declarations, variable declaration etc. This result is called “variableObject” .

In clarity, variables are created here with undefined value. Functions are created here, before the variables declaration.

->Activation/code Execution Stage
Assign values, references to functions and interpret / execute code.
Here code will be executed line by line.

(function(){

console.log(typeof a);
console.log(typeof fullname);
var a=”vinsha”;
var fullname=function(){
return “vinsha kp”;
};

}());

In the above example, we will get the value of “a” as undefined, because variables are created in stage 1 not assigning its value. We will get the value of “fullname” also has “undefined”. because it is also a variable in which a fucntion is assigned.

(function(){

console.log(typeof a);
console.log(typeof fullname);
var a=”vinsha”;
var fullname=function(){
return “vinsha kp”;
};
function a(){
return “fs”;
}

}());

In the above example, we will get the value of “a” as pointing to function a, because functions are created before varibales.Here before var a=”vinsha”;  function a() created. So same name variable declaration is vomited.

we will get the value of “fullname” also has “undefined”. because it is also a variable in which a fucntion is assigned.

So variable and function declarations are hoisted to the top of their function scope. This is called Hoisting in javascript.

Reference and Tanks to :  http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/