How JavaScript code actually executed?

Table of contents
Let me explain when we run the javascript code then what happen behind the scene?
By taking the an easy example let understand
var n=2;
function cube(n){
var ans=n*n*n;
return ans;
}
var cube2=cube(n);
var cube3=cube(2);
when we hit the run button then there are two things happen:
step 1: memory creation phase
step 2: code execution phase
every thing in javascript happen inside the “execution context”
in execution context there are two part memory part and another is code part , in memory part we assign the memory tot he variable and in code part we execute the code.
when the any function is there in the code then the whole code is store in the memory and once the return is hit then the memory is free
and when any function invocation is there then in the memory it stored the undefined. function invocation like see in the code example cube3() and cube2().
as we know javascript is single threaded language then code will executed line by line once the main function is over then function invocation work
when all the code is executed successfully then whole execution context is deleted.
Subscribe to my newsletter
Read articles from hackthic directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
