Lets understand the working with an simple example –
var a = "hello world";
console.log(a);
function demo1() {
console.log("demo1 fun");
demo2();
}
function demo2() {
console.log("demo2 fun");
}
demo1();
console.log("It is GEC");
Now as soon as js engine detects a java script code, it creates an execution context.

Whenever a browser encounters a javascript file, it creates a default execution context that contains all the javascript file that is not inside a function. This default or base execution context is known as the Global Execution Context
Whenever a function is invoked/executed in javascript, a new execution context is created every time inside GEC. This new execution context is known as Function Execution Context
Whenever a js engine run , few things happens –
Pre-requsite to know
Source - link
Execution Context has 2 phase –
Inside the creation phase all the variables are declared as “undefined” and all the functions declaration are stored in the heap memory. This means the variable and function are already made accessible in the current execution context (which is GEC in this case) before the code execution. This concept is known as hoisting.
| Memory Component | Code Component |
|---|---|
| Also know as “Variable Component” | Also know as “Thread of Execution” |
| It hold all the variables and functions as “key :value pair”ex :a =10;b = fuc {…} | Here code is executed one by one in a synchronous single-threaded manner. |
Now in this phase, the whole code will again be executed from top to bottom line by line.
Whenever a java script files is executed, execution context is created. All the compilation and execution of a java script code happens inside the execution content itself.
Few question which interviewer might ask –
- What is execution context?
- How js engine works behind the scene?
- What is hoisting ?
- What is call stack and how it helps in execution ?
- Can we create more than 1 GEC ?
- Can we create more than 1 FEC ?
Try the above question and comment below your answers .
Read this (recommended) – freecodecamp
Advertisement