JavaScript is Single-Threaded

JavaScript is Single-Threaded

Index

Single-Threaded का मतलब क्या है?

Single-threaded होने का मतलब यह है कि JavaScript का execution एक ही thread में होता है, और यह thread sequentially, यानी एक के बाद एक, commands को process करता है।

Example: Simple Code Execution

function firstTask() {
    console.log("This is the first task.");
}

function secondTask() {
    console.log("This is the second task.");
}

firstTask();
secondTask();

Execution Flow (Flow of the Single Thread):

  • JavaScript सबसे पहले firstTask() को execute करेगा।
  • जब यह complete हो जाएगा, तब secondTask() execute होगा।
  • कोई भी दूसरा code तब तक execute नहीं होगा जब तक कि current task (function) पूरा न हो जाए।

Output:


This is the first task.
This is the second task.

Explore More

Blocking Code (Blocking Behavior in Single-Threaded JS)

How Does JavaScript Handle Asynchronous Tasks?

Single-Threaded vs Multi-Threaded

Conclusion

JavaScript एक single-threaded language है, यानी यह एक समय में एक ही task को execute करता है। इसका मतलब है कि सभी tasks sequentially execute होते हैं। हालांकि, asynchronous tasks को efficiently handle करने के लिए JavaScript के पास Event Loop और Callback Queue जैसी powerful mechanisms हैं, जो इसे non-blocking और responsive बनाते हैं।


No comments:

Post a Comment

Most recent Post

Promises in JavaScript

Promises in JavaScript Index What is a Promise? (Promise क्या है?) Syntax of a Promise (Promise का Syntax) Using a Promise...