Why Javascript is single Threaded and Synchronus

Why Javascript is single Threaded and Synchronus

JavaScript Interview cheat Sheet

why JavaScript Single Threaded ?

  • Javascript is Single Threaded Language due to having Single call stack from V8 engine. Which run's javascript in browser. And because It's has a single Call stack it's make javascript Synchronous also.

synchronous.jpg

Why it's good to be Signle Threaded !!

  • Javascript is a single-threaded language becasue while running code on a single thread, it can really easy to implement as we don't have to deal with the complicated scenarios that arise in the multi-threaded environment like deadlock

javascript Single Thread.png

What is Synchronous and how javascript is a Synchronous Language?

  • Synchronous means one at a time. One line of code is being executed at time in order the code appears. And in Javascript due the have a single call stack it's single threaded lanuage, at also execute code line by one so javascript is a Sychronous Language.

If JavaScript is Synchronous so, How we use Asynchronous function's and methods ??

  • Within Javascript we have a lexical environment, syntax parser, an execution context( memory heap and call stack) that is used to executed the JS code. But except these browsers also have Event Loops, callback Queue, and Web APIs that is also used to run Javascript code.

javascript Single Thread2.png

  • as you can see AJAX and Time method are not actually part of javascript but the part of Run Time Environment or browser, so these can be run asynchronously with the Web API using callback queue and going again put in the call stack using event loop to execute