Back to Notes

JavaScript notes

October 09, 2024
4 min read

How JavaScript Works

  • Everything in JavaScript happen inside an Execution context
Execution context
Memorycode
Variable EnvironmentThread Of Execution
Key ValueYour Programs
a:10Where code line by line Execute
fn:{…}

JavaScript Is a Synchronous Single-Threaded Language

Single-Threaded Language - Executed code line by line
Synchronous Single-Threaded Language - Executed code line by line but at specific order

What happen when You run JavaScript code ?

let take a JavaScript Programs

1 var n = 2;
2 function square(num) {
3  var ans = num * num;
4  return ans;
4 }
5 var square2 = square(n);
6 var square4 = square(4);

When Code is Executed it has two phase

1.Memory Allocation phase
Memorycode
n : undefinedat line 1
square:{store whole code of the function}skip 2 to 5
square2:undefinedAt 6 Function Invocation
square4:undefined
In 1st phase of Memory allocation we were allocating memory to variable and function inside the global space (whole program) and we allocated the memory to variable which is undefined in case of function we totally store whole functions code into the memory
2.Code Execution phase
Memorycode
n : 2at line 1
square:{store whole code of the function}skip 2 to 5
square2:4At 6 Function Invocation
square4:16
In 2nd Phase which is Code Execution Phase Now it executed code line by line and
it goes to first line allocate value to it variable and
as control goes to line number 6 ,it’ll invoke the function so when you invoke the function Execution context is created and as it is there 1st Memory allocation num and ans is allocated to memeory and 2nd in Code Execution whole function will run here num * num it store into ans and return statement is executed.In return statement we take back the control over at line number 6 and now square2 will store new value which is coming from function invocation which was the ans
What Do you mean by Function Invocation

It means that function is now begin executed

var square2 = square(n);

Everytime when Function Invoaction there a new Execution Context has been created

Screenshot

As soon as you run the code there a global Execution context is created and

In 1st phase of Memory allocation we were allocating memory to variable and function inside the global space (whole program) and we allocated the memory to variable which is undefined in case of function we totally store whole functions code into the memory

Now in 2nd Phase which is Code Execution Phase Now it executed code line by line and it goes to first line allocate value to it variable and

as control goes to line number 6 ,it’ll invoke the function so when you invoke the function Execution context is created and as it is there 1st Memory allocation num and ans is allocated to memeory and 2nd in Code Execution whole function will run here num * num it store into ans and return statement is executed.In return statement we take back the control over at line number 6 and now square2 will store new value which is coming from function invocation which was the ans

How javascript Engine handle all this execution context creation,deletion and control
  • It Manages a Call Stack

It is stack where a whole(Global) execution context is pushed inside the stack(GEC).

Screenshot

when control goes to function invoaction it create new execution context 1. Which is pushed into stack e.g E1 Once we are done with executing function, we return the ans and poped out E1 and

Screenshot

control goes back to GEC where it left
then we move on line numeber 7,Where the new function is invoked and new execution context is created e.g E2… After executing function, we return the ans and poped out E2 and control goes back to GEC where it left

after all code is executed Whole stack become empty

Screenshot

Call Stack Maintain the order of execution of execution context

ALt Names of Call Stack

  • 1.call Stack
  • 2.Execution Context Stack
  • 3.Program Stack
  • 4.Runtime Stack
  • 5.Machine Stack

Synchronous OR Asynchronous

Single Threaded OR Multi-Threaded

Thank You For Your Time :)