DEV Community

Cover image for Javascript's Single Threaded Nature
ikbal arslan
ikbal arslan

Posted on

Javascript's Single Threaded Nature

Javascript is a single-threaded language let's see what that means.

When the javascript engine starts executing the code it does that synchronously. In this kind of execution, the program is executed line by line, one line at a time. doesn't move to the next line without finishing the execution of the current line.

It can do only one thing at a time

Let's say, you asked for burgers in the restaurant. while you are waiting for your burger you don't do anything else. You start doing other stuff after you get the burger.

var a = 5;
var b = 10;

function sum(a,b){
   return a + b
}

sum()

console.log("hello")
Enter fullscreen mode Exit fullscreen mode

In the code above when we call the function sum we can't move to the console log line until the execution of the sum is done.

There is one way to make the javascript multi-threaded which is with setting up workers. But it doesn't come as default as a developer you should set it up to have multiple execution contexts at the same time.

Top comments (0)