0

As it is obvious that nodeJs is single-threaded. But JavaScript as a language is also single-threaded?

sibabrat swain
  • 1,277
  • 8
  • 20
iUbaid
  • 313
  • 2
  • 16
  • 2
    What does that mean, exactly? _Threads_ as a concept are part of the runtime environment. A programming language is just that, a language. It's like asking if English is single threaded. Could you please clarify your question? – Phil Jun 01 '20 at 05:58
  • @Phil we all ensure of that English is not a programming language. At-least do some comparison with relate able stuff. – iUbaid Jun 01 '20 at 06:04
  • 1
    @iUbaid it would be one thing if you asked if JavaScript _supported_ multi-threading but you specifically said _"JavaScript **as a language**"_. That's why I'm asking for clarification – Phil Jun 01 '20 at 06:06
  • 1
    @Phil How are you relating the term "JavaScript" and "JavaScript as a language" ? – iUbaid Jun 01 '20 at 06:07
  • @iUbaid Sorry, didn't realise it was your question. You really need to clarify what you're asking but I can almost guarantee that it's not about the language itself – Phil Jun 01 '20 at 06:09
  • @Phil In first paragraph and at the last of second line you can see what is mentioned there. https://itnext.io/concurrency-and-asynchronous-behavior-with-rxjs-11b0c4b22597 That is the reason made me ask this question. – iUbaid Jun 01 '20 at 06:15
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215072/discussion-between-iubaid-and-phil). – iUbaid Jun 01 '20 at 06:24
  • 3
    That article is throwing around terms without any sort of consistent meaning. In one paragraph it calls Javascript single threaded and then in another, it shows you how to use threads in Javascript. The language specification itself does not require single-threadedness or multi-threadedness - an environment can choose which to offer. – jfriend00 Jun 01 '20 at 06:27
  • The question this is marked a duplicate of it is REALLY old (2012) and quite out of date and doesn't really answer the precise question here either. That answer predates environments that offer multiple Javascript threads and it predates the multi-threading tools that ARE now part of the Javascript specification. I don't personally see how that OLD answer does anyone any good by marking this as a duplicate of that. Just because the titles look similar does not mean that old answers there are good answers for this question. – jfriend00 Jun 01 '20 at 06:44
  • After all, the point of marking a duplicate should be when the other question has GOOD answers that match the precise point of this question here now in 2020. That 2012 question/answers does NOT do that. – jfriend00 Jun 01 '20 at 06:46
  • What if I run a script.js file in one tab and in another tab run the same script.js tab, will it still use the same thread? – Phantom007 Jul 30 '21 at 07:03
  • "As it is obvious that nodeJs is single-threaded." this is not true - Node both supports userland threads (with worker_threads) as well as use multiple threads under the hood for system interfaces where a non-blocking operation isn't available in a cross-platform way (like file access). – Benjamin Gruenbaum Jan 22 '22 at 16:56

1 Answers1

2

Javascript isn't inherently single-threaded or multi-threaded as a language. There are Javsacript environments that don't offer Javascript threads and environments that do offer Javascript threads. The language itself doesn't specify single-threadedness or multi-threadedness. Instead, it's up to the environment whether it wants to make threads of Javascript available.

So, to your specific question:

Is JavaScript single threaded?

No. It's not specifically single threaded or multiple threaded. The language specification doesn't require either. The threading of Javascript is up to the run-time environment to implement. It so happens that both the browser and node.js started out without Javascript threads and an event-driven architecture which led to this notion that there are no threads in a Javascript environment, but there ARE. They now both offer Javascript threads (WebWorkers in the browser and WorkerThreads in node.js).

The Javascript specification offers some features that are useful in multi-threaded environments such as SharedArrayBuffer objects and Atomics and environments that offer threading provide additional libraries. So, the current language specification recognizes some of the needs of multi-threading and provides some tools for it, though there is no specific requirement that a Javascript runtime has or doesn't have threads. It's up to the run-time whether they want to offer that capability to run multiple Javascript threads of execution in parallel or not.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • WebWorkers are not a JS construction – Kaiido Jun 01 '20 at 06:01
  • 1
    @Kaiido - Well, threads aren't a C++ construction. They're an OS construction that a language and programming environment decides whether they are going to make available. The point is that threads are available for use in multiple Javascript environments. – jfriend00 Jun 01 '20 at 06:02
  • @kaiido they are, the spec has the concept of *agents* (which webworkers are) – Jonas Wilms Jun 01 '20 at 06:02
  • @JonasWilms that JS is aware of different agents like web workers doesn't mean WebWorkers are ruled by JS. Browsers are not written in JS – Kaiido Jun 01 '20 at 06:03
  • @Kaiido - So you think "javascript as a language is inherently single threaded" as the OP asked? What evidence of that can you offer? And, if so, why do we have Javascript threads available in several environments? – jfriend00 Jun 01 '20 at 06:04
  • 1
    I'm not saying that either, I'm saying that your argument that because there are WebWorkers available in an environment and that these Workers are able to run JS means JS is mutli-threaded makes no sense. And actually I'd agree JS as a language at least supports mutlithreading since it exposes SharedArrayBuffer objects and Atomics operations. But that's really not what your answer says. – Kaiido Jun 01 '20 at 06:10
  • @kaiido yes, but through SABs you can actually observe parallel execution of agents (which web workers are an example of). So while this answer is not quite on point, it still holds some truth. – Jonas Wilms Jun 01 '20 at 06:10
  • 1
    @JonasWilms sorry but no, there could very well be Workers without SAB (and there was for some time), nor anything in the language supporting such cases. The MessagePort API is not based on JS at all. JS is just catching on the Web uses, and now can be said to include mutli-threading into account, but that doesn't change the value of that answer to a duplicate answer reopened by a Mjolnir owner just to try to win their few Unicorn Points. – Kaiido Jun 01 '20 at 06:14
  • @Kaiido - I'm not just chasing points here. If I was, I would have deleted my answer as soon as the downvoters pounced. I'm just trying to answer a question and provide more direct help to the question asked than any other answer I saw as a potential dup. So, it appears you're not arguing that Javascript IS single threaded which is the precise question that was asked. So, if it's not single threaded, then what is it? – jfriend00 Jun 01 '20 at 06:17
  • @Kaiido - So it appears that I looked at the question as "Is JavaScript as a language also single-threaded?". I don't see how one could answer anything other than No. The language specification does not require single-threadedness. You seem to think that my pointing to environments that offer multiple Javascript threads as an insufficient argument, but I'm just trying to show that the language does not require single threadedness which is all one needs to know to answer the precise question asked. The language itself doesn't require multi-threads either. It allows both. – jfriend00 Jun 01 '20 at 06:20
  • 1
    Well, is a car a vehicle if you don't drive with it? – Jonas Wilms Jun 01 '20 at 06:20