DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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)

Top comments (0)

Classic DEV Post from 2020:

js visualized

πŸš€βš™οΈ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! πŸ₯³

Happy coding!