Javascript is Synchronous and Single Threaded Language, How Javascript Works?

·

1 min read

Firstly Let's understand meaning of Synchronous - Synchronous means program execute line by line from top to bottom.

Javascript having a Global Execution Context where everything we write in js file is considered. Now Global Execution Context divides into 2 Parts i.e. Variable Environment and Thread Of Execution.

Variable Environment is just a big Jargon in general terms we can consider it as memory where all programs variable stored line by line. Here we store key values.

Thread Of Execution is that part where programs actually runs line by line.

Let's understand with an example :

Screenshot 2021-05-06 at 12.37.35 PM.png

Proceeding Line by Line

            MEMORY                                                              

Line1 num:undefined

Line2 square:whole function

Line3 a:undefined

Line4 b:undefined

            THREAD OF EXECUTION

Line1 num=5

Line2 (It's just a function definition so it would be skip for now)

Line 3 square function calling ,

s1.png

So a new call Stack would be there.

s2.png

Now This Function create a new Global Execution Context for Square function.

s3.png

as return statement is there so it will return to where it is called and pop from call stack.

                a = 25 and programs finishes.

So This is Simple example to understand how javascript works.