DEV Community

Cover image for Mysterious JavaScript Part-I: Synchronous Js
Anik Khan
Anik Khan

Posted on • Updated on

Mysterious JavaScript Part-I: Synchronous Js

Learning is fun. Well, I think it should be written like "Learning is fun๐Ÿฅฑ๐Ÿ˜’"
I don't know who said this but to me, learning is anything but fun. Imagine learning one of JavaScript's mysterious topics like synchronous & asynchronous programming๐Ÿ˜ซ๐Ÿ˜ซ
Could it be any harder?
Well, don't worry. I will try my best to explain some of the inner workings of javascript in a simple way so that you don't have to take the hard path. Join me!๐Ÿ‘ฏโ€โ™‚๏ธ
(Quite a marketing pitch, huh?๐Ÿ˜Ž๐Ÿ˜Ž)

Evolution of JavaScript

JavaScript evolved so much so that it sometimes feels like a different programming language that we used to know.
Alt Text
While this is a good thing, it sometimes can create confusion. One such confusion- Is JavaScript Synchronous or Asynchronous?

JavaScript is Synchronous

Yaay! The truth has been revealed๐Ÿ˜œ. JavaScript at its core- a single-threaded, synchronous language. That basically fancy way of saying- JavaScript does one operation at a time.
While one operation is on run, others stay stand still and wait for their turn.
Alt Text
I hear you saying, what if I try to break this traditional way of executing things? I am glad that you ask. But I believe, in this case, the ladies have the answer๐Ÿ˜๐Ÿ˜œ
Protip: If you want to die sooner, come India and try breaking a line like this (referring the image).

Example

Let's assume that I have three sentences to log in console-

Alt Text

I would get output like this-

Alt Text

The output will be in sequence. JavaScript shows output in sequence.

Hence, if I were to change the sequence of the log-

Alt Text

the output sequence will also be changed-
Alt Text

The point is, JavaScript logs the outputs in a specific order So, changing the order changes the output sequence too. The execution order is tracked & strictly maintained.

Well! Now, the important question- How does JavaScript keep track of this sequence? I mean, we need someone to play the managerial role, right? The manager keeps the rules in place & makes sure that the functions are executed in proper order. Who is this manager?

This million ๐Ÿ’ฒ๐Ÿ’ฒ๐Ÿ’ฒ question will be answered in the next part. So stay tuned ๐Ÿ˜
(images are from unsplash and timesofindia)

Latest comments (0)