Scala.JS will be for Javascript what Scala is for Java

I am writing this article on my way back to New York, after a wonderful nescala 2015 in Boston. Definitely a grand cru. One of the hot topics there was Scala.JS, which is a technology we have recently started to use in banana-rdf. The various discussions and interactions I had during the conference made me realize this:

Prediction: Scala.JS will be for Javascript what Scala is for Java/JVM.

— Alexandre Bertails (@bertails) February 1, 2015

As one could have expected when someone makes such a prediction about programming languages, this sparked an interesting thread on Twitter :-) So let me try to refine what I think the value proposition is for Scala.JS and how I base it on what happened to Scala.

I don’t know many people who got interested in Scala for its own merits (I am not sure I know any…). In fact, we hear many voices pointing out its quirks, and they are real, but that misses the point: I don’t think that Scala would have become as mainstream as it is today if it was not for Java. Many of us came to Scala from Java because it hit a sweet spot: 1) it enables serious functional programming (no, lambdas are not enough…), 2) it gives us a richer and more robust static type system, and 3) it remains completely interoperable with Java. About that last point: we could code in Scala as if it were Java and easily interact with existing libraries.

My claim is that Scala.JS is doing something similar for Javascript, so let’s see how the previous points apply to it.

1) Functional programming has become more prevalent in the IT industry. Developers not only know it exits, they learn its merits and are trained to practice it. Actually, we have seen this trend in Javascript itself and two examples come to my mind: imperative callbacks are being replaced by more composable Promises and immutable datastructures are now a thing. Now, despite the fact that Javascript is becoming more functional, I don’t think it feels very natural yet for FP practitioners, while Scala is already offering a better solution in that area, both in the language itself and in its standard library.

2) I would claim that functional programming becomes interesting only when you are given a way to speak statically about the things you manipulate. This is why a robust and powerful type system is so important for so many people. Scala shines in that area. Look at projects like scala-js-jquery and imagine how easy it becomes to write jQuery code, being guided by the types while having the compiler checking for you that you are using the library correctly.

3) Scala is extremely versatile and captures surprisingly well Javascript’s specificities. At the language level, everything you can do in Javascript can be mapped to Scala almost 1-to-1, and Scala’s dynamic compabilities even let you interact with the lack of types when working with Javascript libraries. Based on my experience, writing typed facades for existing libraries is straightforward, and the main challenge is actually figuring out how to properly use the underlying libraries because there are no types to guide you.

Then there is the obvious stuff: all of a sudden, plenty of efficient immutable datastructures and libraries from the Scala world become available in the browser; tools like IDEs finally become usable with code completion and type checking; the code can be optimized because the types are statically known; and finally, Scala.JS being just Scala, it comes with a rich ecosystem and community.

@bertails Isn't that what Coffee was supposed to be?

— Robin Berjon (@robinberjon) February 1, 2015

@bertails @mandubian Clojure(Script) looks to me a better candidate on top of JS than Scala. much more close (loosely typed, functional)

— GaĆ«tan Renaudeau (@greweb) February 1, 2015

Just like with Java, many people will be happy to write plain Javascript for possibly quite a long time. But let’s say you disagree with one or more of my points above: you still have plenty of contenders to choose from. But my gut feeling is that there is a huge community out there waiting for a compelling alternative that would bring the triptych functional-programming/static-typing/good-js-interop. Scala.JS just hits that sweet spot and the most exciting times are ahead!