Everything in javaSscript happens inside an execution context
- Execution context
Execution context is like an big box and it has two component in it.
The first component is known as memory component so this is a place where a variable and function are store as an key value pair and it is also know as Variable environment
The second component of the execution context is the code component so this is the place where the code is executed one line at a time. and it is also know as thread of execution
JavaScript is a synchronous single threaded Language
The single threaded means that JavaScript can only execute one command at a time. and in a specific order the next line of code will execute after the first line of code. As soon as a JavaScript program run a brand new execution context will created.
It has two component:-
- memory creation phase
- code execution phase
var a = 2;
function square(num){
var ans = num * num;
return ans;
}
var square2 = square(num);
var square4 = square(4)
Memory Creation Phase:
In Memory Creation Phase we are allocating all variable and function in global space inside the whole program and we allocate the variable with undefined and in case of function we just stored the function as it is.Code Execution Phase:
In this phase the JavaScript being single threaded language will run line by line and allocate the value of a variable and update the values of variable and function.
Top comments (0)