Scala vs Javascript (ES6, TypeScript, Node.js)

Now with Scala.js allowing isomorphic Scala code to run on both browser and server. Scala contends with JavaScript in the web domain. But assuming we are talking about web, how do the two languages compare?

First, JavaScript comes in a few flavors. There's ES5 which is currently running in all browsers. There's Node.js, which is the same Google V8 JavaScript engine but running outside the browser and thus without browser/web apis included, but which can run ES6 (EcmaScript 6). And then there are the many languages that compile or transpile down to plain old ES5 JavaScript, including ES6 itself (via Babel), TypeScript (a strongly-typed superset of JavaScript), CoffeeScript (a nicer syntax for ES5), and more.

Scala would technically be in this list, as a language which, via Scala.js, can transpile down to plain JS. But it's not based on JavaScript. And it's a language which has a reputation for being difficult to learn compared to JavaScript. However, developers familiar with ES6 will find much of Scala to be analogous to ES6 syntax. Both languages (since ES6) have variable evaluation in strings (only in template strings in ES6). Like ES6, Scala has mutable (let, var vs var) and immutable (const vs val) variables. Scala, like ES6, uses fat arrows to create "arrow functions", which are the type of JS expression which return a value without an explicit return - how all Scala functions already work. JavaScript's list/array literal syntax is like Python's whereas Scala's is like PHP's. ES6 and Scala support default values for function arguments. ES6 requires passing in an object to use named arguments for out-of-order or skipping arguments. Both languages support destructuring (assigning many variables to the values of a complex object or array). Like the standardizing Promises in newer JavaScript implementations, Scala has Futures for reasoning about asynchronous sequences of processing. Scala's if() statement is actually a function which returns a value. And then Scala has some strange-seeming constructs which aren't found in languages which JavaScript programmers may be used to: for-comprehensions (see list comprehensions in JavaScript), the pattern matching match conditional, singletons, and, of course, explicit types!

OK, so a JavaScript developer who learns ES6 is well on their way to understanding Scala. There are other things you would have to get used to, like slow build times (sbt), a lack of community around you to benefit from in learning, calls to the underlying Java api (Scala, when not compiling to JavaScript, compiles to Java and makes use of Java calls which require knowledge of Java).

Node.js developers who have build scripts which run JS linters / static code analysis or transpile from one form to ES6, minify, etc. are used to having to wait between saving a file and running and testing the new code. But in general, the interpreted world of JavaScript, Python, PHP, etc. let you save and test in a quick feedback loop. With things like HMR (hot module replacement) in Node.js, it's not even necessary to refresh a web page after changing code. While JavaScript code runs in a JS runtime, and Java code runs in a JVM, Java code needs to be compiled to bytecode first in order to run, and Scala is no different. Each line of code changed requires scalac to compile back to Java bytecode before it can be executed, and scalac is notoriously slow.

One of the benefits of this slow compilation in Scala is the type-checking which the language can do during this step, catching certain classes of programming errors. One can argue that for large code bases spanning years of development and maintenance by non-originating junior developers that this type safety is a huge benefit. And that some of the unique constructs in Scala are worth spending the time up front learning for the improvements in relative coding efficiency (if they exist).

But the benefits of things like pattern matching are negligible for web projects done by web developers who benefit much more from being part of a huge developer ecosystem and Scala doesn't have this. There may be dozens of frontend web frameworks but they're all JavaScript. For web development on the server, Node has tons of packages. Scala is missing so many useful libraries for web. JavaScript developers are everywhere. Scala developers are nowhere to be found. One can make arguments why Scala is technically a better language than JavaScript but their voices are being drowned out by the millions of jQuery/Node.js/etc. developers getting the job done right now.