In today's post, I am going to write about how javascript works. We will throw some light on terminologies like Execution context, function level execution context, function invoking, and threads.
**Do you know what happens behind the scene when you run a javascript code? **
Let's take an example of the below code snippet.
https://gist.github.com/c8dc2561b8078487a6fdfd0fd9f0cc95
EC : Execution Context
When we run this code, here is what the Javascript engine does.
-
First, a
Global Execution Context
is going to be created. -
Interpreter encounters
call x()
, and again a new EC is created for x. -
Now the
EC
for x is created it will run the code line by line inside x. -
Inside x, a new function call
y()
is invoked, it again creates an EC for y. -
When y is finished running, it will back to EC of
x
, and variable t is assigned. -
After x run is finished, it will be back to
Global EC
, and variable r is assigned. -
Global EC
finishes running and the program stops.
In tomorrow's blog, I will explain how the call stack works in Javascript. I would love to know your feedback on this blog.
Stay tuned - Follow me on Twitter