1

As per the title of this question...why does JavaScript seem so different than Java? Seem being the operative, subjective linking verb...

Essentially, why do novices often assume that JavaScript is asynchronous by default? What is the quality of JavaScript that leads beginners to make this assumption compared to Java, where such an assumption is never made? Does this have something to do with concurrency in the browser event loop?

Sean Van Gorder
  • 3,393
  • 26
  • 26
Martin Erlic
  • 5,467
  • 22
  • 81
  • 153
  • 2
    JavaScript has nothing to do with Java. And [tag:nodejs] is an asynchronous framework, so they may well be coming from that environment (but such is *speculation*). – Elliott Frisch Jul 11 '17 at 16:27
  • I'm just wondering why novices think JavaScript is asynchronous by default when they don't think the same thing about Java. All else equal, I don't think it's just about being misinformed. Too many people make the same incorrect assumptions. – Martin Erlic Jul 11 '17 at 16:28
  • _"why do novices often assume that JavaScript is asynchronous by default"_ I don't think they do, seeing the number of questions that usually get closed as duplicates for [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Evans Jul 11 '17 at 16:30
  • 2
    Because Java and JavaScript aren't the same language. Why should they think Java is async just because they think JS is? – Vince Jul 11 '17 at 16:31
  • I guess the comparison doesn't make sense, but it's not a comparison between langues per se; rather, the difference between how you learn them. There's a "quality" that JavaScript has that in my experience leads lots of people to claim it's "more asynchronous" (however dumb this sounds). – Martin Erlic Jul 11 '17 at 16:32
  • 2
    It could be that, being a functional language, JavaScript usually requires lots of callback functions, which give the appearance of being asynchronous (especially in frameworks like node.js and query). – Andrew Farm Jul 11 '17 at 16:33
  • @AndrewFarm Yeah that would seem to make sense. – Martin Erlic Jul 11 '17 at 16:36
  • @AndrewFarm Pedantic: JavaScript is *not* a functional language. JavaScript *does* have higher-order functions and closures which are *concepts* found in functional languages. – user2864740 Jul 11 '17 at 16:37
  • 1
    In my experience, novices _don't_ think it's asynchronous. But at some point they run into problems with callbacks where it is explained to them that some of their code is run only after the other part has long finished. I speculate that this then brings up the question of _when_ that code will actually run, and what the system guarantees or not. – Siguza Jul 11 '17 at 17:02

2 Answers2

2

Java and JavaScript are two different languages and, despite some similarities in syntax and nomenclature, are designed and operate much differently. (JavaScript is Mozilla's implementation of the ECMAScript specification, although it's used colloquially like "Xerox" or "Saran Wrap".)

Java supports multithreaded; the JavaScript (ECMAScript) specification does define threads or multithreading.

JavaScript OO is prototype based and methods are unbound; Java is message based and all methods are bound to objects.

JavaScript is dynamically (and generally weakly) typed; Java is statically and strongly typed.

JavaScript is interpreted in that source code is executed directly; Java must be compiled first.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Fabiano Taioli
  • 5,270
  • 1
  • 35
  • 49
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/About_JavaScript - "Contrary to popular misconception, **JavaScript is not "Interpreted Java".** In a nutshell, JavaScript is a dynamic scripting language supporting prototype based object construction. The basic syntax is intentionally similar to both Java and C++ to reduce the number of new concepts required to learn the language..." and "... **JavaScript® is a trademark or registered trademark of Oracle** in the U.S. and other countries." – user2864740 Jul 11 '17 at 16:49
1

One question stuck out as "really out of place", so I'll answer that (and that only):

Essentially, why do novices often assume that JavaScript is asynchronous by default? What is the quality of JavaScript that leads beginners to make this assumption compared to Java, where such an assumption is never made?

JavaScript (henceforth referred to as ECMAScript or ES) is predominantly used for two purposes:

  1. websites (or other browser hosted) and;
  2. node.js.

In both of these contexts the underlying host infrastructure exists to take advantage of ES's support for first-class functions and closures: Browser Interaction Events, setTimeout, AJAX, Web Workers, and the slew of async support in Node.

(Neither first-class functions nor closures are required for asynchronous programming; they just make it 'really easy' in JavaScript.)

Thus, most programmers never know an environment where the host does not support native constructs that allow asynchronous programming; and support for asynchronous callbacks is used/required to make most programs of interest.

However, if the host does not expose any asynchronous methods, then "asynchronous by default" would be shown to an addition leveraging the ES language features:

ES program execution is always synchronous (this is why a callback must run to completion and cannot be pre-empted) although these tiny synchronous blocks can be interleaved.

user2864740
  • 60,010
  • 15
  • 145
  • 220