Is JavaScript multithreaded?

Javascript is not multi-threaded. It is a single threaded programming language.

So what does it mean?

It means javascript performs only one task at any given time. It can not do two things at the same time. This is the core behavior of javascript language.

const first = “print me first”;

function printMe(){
  console.log(“print me second”)

// print me second

// print me first

In the example above, when we run the program javascript will parse through the codes line by line and execute one code at a time from top to bottom order. So, here it will first save const first and function printMe in memory and then assign string value “print me first” to const first.  After that it will call printMe() function and lastly console log const first. Javascript will not move to next line of code until it has executed the code it is currently at.

Let’s try another example.

function x(){
  prompt(“enter your name”);
  console.log(“print me next”);

When you call function x, prompt will be processed first. Javascript will not console log “print me next” until user responds to prompt. It can not wait for user response and execute next line of code at the same time. It can not multitask.

One of the advantage of javascript being single threaded is that it is predictable. We know that javascript is going to execute the code in the sequence that we have written and only one line of javascript code is running.