Javascript is Synchronous and Single Threaded Language, How Javascript Works?
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 :
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 ,
So a new call Stack would be there.
Now This Function create a new Global Execution Context for Square function.
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.