DEV Community

Cover image for JavaScript is a single threaded "Synchronous", What does that mean?!
Ahmad Mukahal
Ahmad Mukahal

Posted on

JavaScript is a single threaded "Synchronous", What does that mean?!

Hello everyone, in this article I will give you the mean of single threaded javascript.

First, let's talk about the JavaScript engine in brief way.

A JavaScript engine is a software component that executes JavaScript code, Its consists of many steps and components to allow it perform it's tasks.

The two main important things in this step are:

1- We need to store and write information/data for our application (variables, objects, etc..).

2- We need to keep track of what's happening to our code line by line.

This is where a Call stack and Memory heap comes in.

This image explain this two component in graphical way :

Memory heap and call stack

1. Call stack:

Help to know where we are in the code and to keep track of its place in a script that calls multiple functions β€” what function is currently being run and what functions are called from within that function, etc.
To know more about call stack and how it is work exactly, I recommend this tutorial for you.

2. Memory heap:

The memory heap, also called the β€˜heap’, is a section of unstructured memory that is used for the allocation of objects and variables, so it is where our variables and functions stores Briefly.
To deep in heap from here

After that, back to our main subject, "Javascript is a single threaded programming language" which means it has only one call stack that is used to execute the program, so one set of instructions is executed at a time, it's not doing multiple things.
And because of that JavaScript is Synchronous.

So if you understand what is single threaded means, it's the same concept with Synchronous JavaScript "one thing at a time".

This approach of programming lead to make many problems, so the direction now to use another way of JavaScript called "Asynchronous" programming.
I will make to it another article in the come in days.

Hope you clearly understand this important concepts as a JavaScript developer! πŸ™ŒπŸŒΉ

  • Ahmad Mukahal

Top comments (1)

Collapse
 
seek4samurai profile image
Gourav Singh Rawat

When it comes to Asynchronous at backend I guess NodeJS could be helpful.
Since NodeJS uses asynchronous callbacks not like one Database queries, it doesn't have to wait for one set of instruction to get completed.