Javascript Interview Preparation CheetSheet

Javascript Interview Preparation CheetSheet

Javascript Interview Topics - Single Threade Language

The most frequently asked questions in Javascript Interviews are basically from some core topics of Javascript, which are Call Stack, Scope Chain, Multi-Paradigm, Single Thread , Hoisting, Prototype-based Object Oriented and 'this' -keyword.

Let's discuss "why Javascript is Single Threaded Language"?

Let's start the discussion with some definitions of Javascript language. So, here I find a pretty brief line on the internet.

Javascript is Single Threaded and Non-Blocking Event Loop Concurrency Model.

So, we should start explaining from here line by line.

First, what actually is a concurrency model?

It simply means how the JavaScript engine handles multiple tasks happening at the same time.

Now, another question arises here, what is the need for this?

Well, it's because JavaScript itself runs in one single-thread.

Again a fancy word :-),

Well Single thread means that it can only do one thing at a time

So, the statement is now formed like, Because Javascript is a Single Threaded Language therefore we need a way of handling multiple things happening at the same time.

A quick note here,

Thread is a set of instructions, that is executed in a machine's CPU. it's where our code is executed in the processor.

But what will happen in long-running tasks like fetching data from a remote server?

It seems like that would block the single thread where the code is running, But it won't happen...Why So?

It's because of the non-blocking behavior of Javascript Language

But how do we get that non-blocking behavior?

Well, It is by using a so-called Event Loop.

So, What is the Event loop does?

In simple terms, the event loop takes long-running tasks, executes them in the background, and then puts them back in the main thread once they are finished and this is, in a nutshell, JavaScript's non-blocking event loop concurrency model with a single thread.

But behind the scene, it's not so easy task as we have written here, it's basically an oversimplification of this complex topic.

As we progress in the article, we will again touch on this topic later when we really understand about event loop in more detail.

So, follow me in this Javascript world !!