Why Scala.js is preferred language for front-end development

Table of contents
Reading Time: 4 minutes

From last few years, I have been working on Scala as a back-end and Javascript as front-end for web application.  It becomes very painful, when I have to refactor a large Javascript code base, written by someone else. There are also many annoying language warts. I tried to use other front-end framework, targeting JS but wanted to use same platform to go all the way from server to client.  I started learning Scala.js, I found it to be much more helpful, powerful language with full of amazing features as compare to any other front-end development framework. Below are the few reasons, which will attract you towards Scala.js.

1) Write Scala, not Javascript
One of the major reason to use Scala.js, you can write code in scala, which will be compiled into JavaScript. You can get the benefit of a functional language, immutability, powerful collection, pattern matching, type-safety etc.  If your application’s back-end part is written in Scala, you don’t need to fall back to JavaScript for your client side code. Even if you are not a web developer, you can start doing development in JavaScript without knowing JavaScript.

2) Statically Typed Language
Since Javascript is dynamic typed language, so types are checked at run-time. Due to this, most of the time we get silly typo errors while programming in Javascript. A variable could be assigned to Integer and later reassigned to String.
unnamed (6).png
In contrast to JS, Scala.js is statically typed language with type inference and due to its compiler, you can stop worrying about silly type errors. You can not mix up String with Integer.
unnamed
However there are also other statically typed languages available, which get compiled into JS such as GWT, Dart, TypeScript etc . You can find list here. But beauty of Scala.js makes it preferred language over these languages, which is being described in this blog.

3) Excellent IDE support
Scala.js has excellent IDE support such as IntelliJ and Eclipse, which will help in error highlighting, catching type-error without compiling your code, refactoring field and methods.
unnamed

​4) Easy Refactoring
Refactoring is very hard in dynamically types language. Even compiler and IDEs would support less, if language is dynamic. In case of Javascript, you can do easy refactoring, if you have only few lines of code or hundred lines of code. In large code-base, it is hard to find which field or method was called, when something is broken on UI.

5) Interoperability with  JavaScript code
This is one of the powerful feature of Scala.js. There is rich library collection, available for Scala.js such as scala-js-dom, scalajs-jquery etc. You can use Javascript library directly from your Scala code either in a statically or dynamically typed way. e.g. to build Angular based application with Scala language, you can use scalajs-angular library.

6) Shared Code
I have never been thought of writing code in same language on client and server side. If server side code was written in PHP, java, or scala, there was always JavaScript on client side. Although Node.js gives you  flexibility to write code in same language on client side and server side. But in this case, you have to write everything in javascript.
You can use same library, algorithm on both client and server side.

7) No JavaScript Warts
In JavaScript, sometimes we face surprising behavior.
e.g.- There is problem with Lexical block scope in Javascript
unnamed (1)
However this is called variable hoisting in Javascript. Its kind of annoying and confusing. There is an alternate to fix this. Using ‘with’ you can fix it.
Below is same code written in Scala-

unnamed (2)

One of the annoying wart of Javascript is equality confusion

unnamed (3)

It is best to use ===  or !==

unnamed (4)

In Scala.js, you would never face these kind of warts.
unnamed (5)

There are many other reasons to use Scala.js for front-end development. I am still exploring Scala.js.  If you want to dig more about Scala.js, you can have a look at www.scala-js.org.

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.

15 thoughts on “Why Scala.js is preferred language for front-end development3 min read

  1. Hi Ayush,

    Nice article! (But I have a diff in opinion)

    To start with, I am a java programmer (not scala). I have larger exp with AngularJS and JQuery (long back).

    Now, it difficult for me to appreciate the #1 and 6 above 🙁

    If I am writing my code in AngularJS, I do have good industry support. This is missing with scalaJS (not as many programmers).

    ScalaJS try to bridge with Angular, but I dont see a need from Angular to do the same for scalaJS.

    Regards,
    Sachin

    1. Thanks for your feedback. I do agree with your opinion. If you are developing your application in Scala, #1 and #6 would be more beneficial and you don’t need to learn any other language .
      It doesn’t matter whether your back-end is written in Scala.
      If you want to use type safety, immutability, powerful collection, pattern matching,
      If you want to get rid of silly typo errors and language warts,
      If you want better IDE support,
      then you should go head with Scala.js. Choice is yours 🙂

  2. Many of your JS warts go away if you use TS. I’m a big fan of Scala but TS feels close enough to Scala for me. It also compiles to clean JS – the output of Scala.js was much harder to follow.

  3. A lot of people prefer writing plain Javascript for the front-end because …. they actually like the language and they like writing Javascript, and not something that compiles into Javascript.

    I can understand the need for type safety and immutable objects and so forth, but the benefit and potentially downfall of Javascript is its flexibility. People hate it for its flexibility but once you understand how to fully utilize it, most JS developers I know wouldn’t want to use another language for front end development.

    1. Hi Olu,

      Being a developer who has worked in both js and scala.js world I think I can answer your question. There was a time when I worked with ember and angular area. It was great. These frameworks has proven themselves in test of time and had done so for uncountable application out there. But here is the deal, even if these framework gives you a lot of power you as a javascript developer have to be very disciplined. You have to maintain same discipline thoroghout your whole team, which is not a very big problem if team is small but once size grows so is your effort in maintaining the application. And that is a problem with a js application. If you are not great with framework or js in general you are basically shooting in your own leg. Scala.js on the other hand enforce this discipline through type safety and strict typing. That way it is guaranteed to have this discipline during development. Plus you get to code in a language which has a proven record in functional realms. And again as Ayush pointed you get to share code between server and client. Isn’t it beautiful. Now you can argue with that we have such support with node.js as well but again even in that ecosystem you have to control the dynamic js beast with a pleonthra of callbacks. And many have started noticing this limiting factor and had taken the course of traditional scala or java based servers. To me as developer what I see a great advantage is the catching errors at compile time. It saves a great deal of time and frustration something you can’t avoid with js.

  4. Hi Ayush,
    Being a developer working full time on scala.js application I can’t agree to all of your points above. Great insights. However besides typesafety and functional advantages it offers another advantage is the ecosystem.scala.js has matured to a great level. Libraries like upickle and boopickle take care of json parsing, play-scalajs has a brilliant build setup to integrate scalajs application with the play server, scala diode is a brilliant library to setup the immutable state architecture for the application, scalajs-react is a brilliant wrapper around react js and does hell of a job for building an application and sometimes even better, then there are templating libraries like scalatags and scalacss which brings type safety to styling and html as well(Typesafe html and css brilliant isn’t it!!!!) . And then there is facade libraries for many library out there in js world (three.js, angular,js, bootstrap, selectize.js, datepicker,js, you name it!!!). So in nutshell it has great capabilities. I am working on an open source project and have used all of them. Take a look at it here https://github.com/LivelyGig/ProductWebUI. I would love to connect with knoldus on this front. Have a great time with scala.js 🙂

  5. so you replace a simple language with lots of wart by a complex language with lots of warts?
    With all the option that compiles to js we have (ruby, kotlin, F#, ocaml and even haskell), why bother with such an ugly language?

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading