Javascript Global Execution Context
By Saket Bhatnagar••Beginner to Intermediate
global execution context
- 1When we give JS code to the browser, JS Engine will allocate (create) a global memory block for the execution of JavaScript code, called Global Execution Context.
- 2 Here, we have a window variable which have reference of Global Execution Context.
window variable
- 1Window variable or window object -> everything is object in js.
- 2Window is a global variable which store the reference of Global Execution Context
- 3Window object is also known as Global Object because it is available anywhere in the program.
- 4Window object have pre-defined state and behaviour.
- 5 Variable declared with var always goes to global scope and can be accessible by window object.
- 6 Any variable created in global scope will be addes in Window object implicitly by JS Engine.
JavaScript code run in two phases
- 1Variable phase
- 2Execution phase
Variable Phase
- 1In variable phase, JS Engine will check the complete JS Code and it will search for variable declaration statement.
- 2 If variable is declared then JS Engine allocate (provide) memory for them.
- 3Variable declared with var will be initialized storing "undefined" at the time of memory block creation.
Variable declared with let and const will remain uninitialized (empty) at the time of memory block creation.
Execution Phase
- 1In Execution phase, JS Engine will execute the instruction line-by-line.