DEV Community

Edison Ade
Edison Ade

Posted on

Is JavaScript Synchronous or Asynchronous?

Please share your thoughts on this. Someone might learn from your submission.

Top comments (6)

shahrozahmd profile image
Shahroz Ahmed

javascript is synchronous by nature but it has a lot of features like callbacks, promises, async/await which allows you to implement asynchronous event handling in your project... hope you get the point :)

adnanbabakan profile image
Adnan Babakan (he/him) • Edited

Let's not confuse a language's itself with its features!
JavaScript is synchronous itself but features ways of dealing with functions we don't know when they are going to be called.
JavaScript has always been synchronous and single-threaded! When you are running a JavaScript block of code on a page no other block will be executed on that page at the same time.
JavaScript is synchronous when it comes to execution. Look at the code below:


let x = 5

This is going to throw an error since x is not defined when we are trying to log it.

Check this image out:
JavaScript timer

This will help you understand how JavaScript works.

And for more information check this article out:
How JavaScript Timers Work

mburszley profile image
Maximilian Burszley

Please don't conflate JavaScript with its engines. That is how the event loop works in a browser. It may not necessarily be the same as NodeJS for example.

adnanbabakan profile image
Adnan Babakan (he/him)

I believe Nodejs is no different than the JavaScript that is run in the browsers. Nodejs is running under V8 engine and that acts pretty similar to browser JavaScript. It is single threaded and also synchronous.

calix profile image
Calix Huang

Javascript can be either asynchronous or synchronous. Javascript is most known for being asynchronous because it can be, while other programming languages are multi-threaded at best. It is a huge advantage over other languages because it's so much faster.

mburszley profile image
Maximilian Burszley • Edited

You're confusing a lot of concepts here. Concurrency (async) has nothing to do with threading vs multiprocessing or whatever model you use. It's about "parallel" execution. JavaScript is synchronous by default. JavaScript is just a language, it is neither fast or slow. That is up to its engines.