Today on Js ( 1 - 31/03/24)
JS Execution context
There are two types of JS execution context
Global execution context
Function execution context
Eval execution context ( won't go in-depth here)
Global Execution context
the global execution context is different for every engine or browser for example, if you go into Chrome and check its GEC you will see a lot of Windows event listeners and such
{} - these curly braces define the GEC
Function Execution context
When a function is called, the JS engine creates an FEC within GEC to execute the code within that function
How does it work? (Overview)
It runs in two phases
Memory creation phase -> allocation of memory
Execution phase -> stuff like arithmetic operation is done here
for example :
let val1 = 10
let val2 = 5
function addNum(num1,num2){
let total = num1+num2
return total
}
let result1 = addNum(val1,val2)
let result2 = addNum(5,3)
1-> Global Execution -"this" stores GE 2-> Memory Phase (the steps in which the above is stored ) val1 -> undefined val2 -> undefined addNum -> definition of the function result1 -> undefined result2 -> undefined // This is the first cycle
3-> Execution Phase val1 -> 10 val2 -> 5 // addNum will execute nothing as this is an execution phase so addnum will not do anything // this addNum will make its context box called new executional context which will contain a new variable environment + execution thread every time a function is called a new executional context will be created; when a sandbox(execution context) the memory and execution phase will be repeated So, Memory Phase -> val1 -> undefined val2 -> undefined total -> undefined
Execution context -> processing is done, data manipulating, etc is done num1 -> 10 num2 -> 5 Note: after GE is used it automatically gets deleted as well total -> 15
now it goes back to the previous memory phase and does result = 15 now same stuff with result2 new variable environments and a thread with its two-phase