Explain What Single-Threaded JavaScript Means Like You Are 5 Years Old

You are 5. A couple of years ago you could barely walk and talk. And now you’re going to learn the hardest concept in JavaScript – threads. Sounds cool? Let’s start.

Single-Threaded Javascript. Our Super. Special. Kid.

JavaScript is differe…


This content originally appeared on DEV Community and was authored by Nick Bull

You are 5. A couple of years ago you could barely walk and talk. And now you're going to learn the hardest concept in JavaScript – threads. Sounds cool? Let’s start.

Single-Threaded Javascript. Our Super. Special. Kid.

JavaScript is different from all other children. He is a special kid.

Slide 16_9 - 1.png

No, he can’t fly. He is single-threaded. While other kids are multi-threaded.

Nothing bad about it. It’s his choice. I even love it. But that's what makes it completely different in terms of how the code is running in JavaScript.

When normal kids want to make a hot chocolate and watch a funny show on YouTube, they do it at the same time:

Slide 16_9 - 12.png

But when our special kid wants to do it, he does it one by one:

Slide 16_9 - 13.png

The difference is that our kid always waits until some task is done and only after that starts the next one. Other kids perform tasks simultaneously and don’t wait until some task is completed.

The same way single-thread languages are different from multiple-thread languages. One performs only one task at a time. Others can perform them in parallel.

But why single-thread languages, special kids, can’t be normal and perform tasks in parallel?

It's all about how the threads work.

Threads. Slides. Dogs.

In programming, threads are essentially single processes that a program can use to perform tasks. Like a water slide where performing a task is the process of sliding down.

Slide 16_9 - 10.png

Yes, it's a dog. It represents a task.

Your computer works that way that each thread can execute only one task at a time. And everything else is blocked until an operation completes.

When one dog slides down a water slide, the other dogs have to wait. They can't slide down. Only one dog in a water slide. Only one task at a time in a thread.

Slide 16_9 - 11.png

Our special kid JavaScript is a single-threaded language. It has only one thread. That’s why it can perform only one task at a time.

But other kids are different.

Thread. Aquapark. Our child has grown up.

Imagine that our special kid grew up and decided to open an aquapark for dogs. But not a normal aquapark. He applied his “single-threaded philosophy of life" to it.

Slide 16_9 - 4.png

Because JavaScript is a single-threaded aquapark that only has one water slide, dogs can't go and have fun on the other slides. They have to stand in line and wait for each dog in front of them to finish riding.

Slide 16_9 - 5.png

Looking at this, I can tell that our special kid JavaScript is going to be a total business fiasco. Can you imagine what will happen if some dog will slide down the water slide for 1 hour? Other dogs are waiting. Owners are angry. Dog Guards can't go home. Total disaster.

Slide 16_9 - 6.png

Not every time we want to wait until some task is complete to move on to the next one. Sometimes we want to do them in parallel. Like other kids do. But JavaScript can't do it. He is a special kid. He is single-threaded.

And no matter how hard he tries to imitate multithreading behavior with setTimeout(..., 0) and web workers, he will still remain single-threaded. He will still remain our special kid.

Slide 16_9 - 9.png

PS: no dogs were harmed in the process of writing this article.

In the end...

If you want to learn JavaScript, I have something interesting for you. A JavaScript course that fixes one thing that 97% of other courses lack – a combination of modern and practical JavaScript theory with real-world practice.

Try to get it here: javascriptcoursethatworks.com

And if you like this article you need to know that every Monday, I send a letter to 4,000+ Web Developers with 3 hand-picked articles from the tech world, 2 web development guides, and 1 best Tweet of the week

“Your newsletter is fking awesome, really Nick. It gives me a great start to the week with a lot of insightful knowledge.”

Join smart developers who get short and full of knowledge letters for free here.


This content originally appeared on DEV Community and was authored by Nick Bull


Print Share Comment Cite Upload Translate Updates
APA

Nick Bull | Sciencx (2021-06-05T12:26:46+00:00) Explain What Single-Threaded JavaScript Means Like You Are 5 Years Old. Retrieved from https://www.scien.cx/2021/06/05/explain-what-single-threaded-javascript-means-like-you-are-5-years-old/

MLA
" » Explain What Single-Threaded JavaScript Means Like You Are 5 Years Old." Nick Bull | Sciencx - Saturday June 5, 2021, https://www.scien.cx/2021/06/05/explain-what-single-threaded-javascript-means-like-you-are-5-years-old/
HARVARD
Nick Bull | Sciencx Saturday June 5, 2021 » Explain What Single-Threaded JavaScript Means Like You Are 5 Years Old., viewed ,<https://www.scien.cx/2021/06/05/explain-what-single-threaded-javascript-means-like-you-are-5-years-old/>
VANCOUVER
Nick Bull | Sciencx - » Explain What Single-Threaded JavaScript Means Like You Are 5 Years Old. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/06/05/explain-what-single-threaded-javascript-means-like-you-are-5-years-old/
CHICAGO
" » Explain What Single-Threaded JavaScript Means Like You Are 5 Years Old." Nick Bull | Sciencx - Accessed . https://www.scien.cx/2021/06/05/explain-what-single-threaded-javascript-means-like-you-are-5-years-old/
IEEE
" » Explain What Single-Threaded JavaScript Means Like You Are 5 Years Old." Nick Bull | Sciencx [Online]. Available: https://www.scien.cx/2021/06/05/explain-what-single-threaded-javascript-means-like-you-are-5-years-old/. [Accessed: ]
rf:citation
» Explain What Single-Threaded JavaScript Means Like You Are 5 Years Old | Nick Bull | Sciencx | https://www.scien.cx/2021/06/05/explain-what-single-threaded-javascript-means-like-you-are-5-years-old/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.