#2 Why js is a single threaded language

1.JavaScript is an interpreted language, not a compiled one. This means that it needs an interpreter which converts the JS code to a machine code(bits of 0 and 1).

2.There are several types of interpreters (known as engines). The most popular browser …


This content originally appeared on DEV Community and was authored by Arun

1.JavaScript is an interpreted language, not a compiled one. This means that it needs an interpreter which converts the JS code to a machine code(bits of 0 and 1).

2.There are several types of interpreters (known as engines). The most popular browser engines are V8 (Chrome), Quantum (Firefox) and WebKit (Safari).

3.Each engine contains a memory heap, a call stack, an event loop, a callback queue and a WebAPI with HTTP requests, timers, events.

4.A single threaded language is the one with single call stack and a memory heap.It means that it runs only one thing at a time.

What is a stack?

1.Stack is a fundamental data structure in computer science that operates in LIFO (Last In First Out) principle.

For Example=> a stack of plates; the last plate you put on is the first one you take off. This means the most recently added element is the first one to be removed.

2.Stack has two primary operations
Push and pop. The Former adds new element to the top of the stack and the latter removes and returns the element from the stack.

How Does JavaScript Achieve Non-Blocking Behavior?

1.JavaScript is a single-threaded language and, at the same time, also non-blocking, asynchronous and concurrent.

2.Though it runs in a single thread, in can still perform tasks asynchronously without blocking the main thread.

3.This is achieved through event loops, call back queues and asynchronous APIs provided by the environment (browser).

Event Loop

1.Event Loop is a mechanism that allows JS to handle asynchronous operations while running in a single thread.

  1. It is responsible for managing execution of codes,events and messages in a non-blocking manner.

Let us understand how it works

1.Call Stack JS starts pushing the execution context of the function into the call stack one at a time in LIFO fashion.

2.Web API When JS comes across the asynchronous operations like settimeout() , fetch() or I/O operations it assigns this tasks to Web APIs in the browser.

3.Callback Queue After the async operations is complete, the callback fuction that was passed as an argument is added to the call back queue.

4.Event Loop The event loop constantly moniters the call stack and the calback queue. When the moment, the call stack is empty the event loop pushes the first callback from the queue to the call stack for execution.

Example

console.log("Start");

setTimeout(() => {
  console.log("This is asynchronous.");
}, 2000);

console.log("End");

O/P:
Start
End
This is asynchronous.

You can watch the below video for more insight into the concept.

https://youtu.be/HDFccoO992o?si=OCBoSVSekJ9VoO7I


This content originally appeared on DEV Community and was authored by Arun


Print Share Comment Cite Upload Translate Updates
APA

Arun | Sciencx (2025-06-28T13:54:26+00:00) #2 Why js is a single threaded language. Retrieved from https://www.scien.cx/2025/06/28/2-why-js-is-a-single-threaded-language/

MLA
" » #2 Why js is a single threaded language." Arun | Sciencx - Saturday June 28, 2025, https://www.scien.cx/2025/06/28/2-why-js-is-a-single-threaded-language/
HARVARD
Arun | Sciencx Saturday June 28, 2025 » #2 Why js is a single threaded language., viewed ,<https://www.scien.cx/2025/06/28/2-why-js-is-a-single-threaded-language/>
VANCOUVER
Arun | Sciencx - » #2 Why js is a single threaded language. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/06/28/2-why-js-is-a-single-threaded-language/
CHICAGO
" » #2 Why js is a single threaded language." Arun | Sciencx - Accessed . https://www.scien.cx/2025/06/28/2-why-js-is-a-single-threaded-language/
IEEE
" » #2 Why js is a single threaded language." Arun | Sciencx [Online]. Available: https://www.scien.cx/2025/06/28/2-why-js-is-a-single-threaded-language/. [Accessed: ]
rf:citation
» #2 Why js is a single threaded language | Arun | Sciencx | https://www.scien.cx/2025/06/28/2-why-js-is-a-single-threaded-language/ |

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.