5

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

 3 years ago
source link: https://blog.knoldus.com/why-scala-js-is-preferred-language-for-front-end-development/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK