Mysterious JavaScript Part-I: Synchronous Js
Anik Khan
Posted on July 10, 2020
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.
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.
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-
I would get output like this-
The output will be in sequence. JavaScript shows output in sequence.
Hence, if I were to change the sequence of the log-
the output sequence will also be changed-
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)
Posted on July 10, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 10, 2023