SlideShare a Scribd company logo
Intro to Scala.js
Marius Soutier
Freelance Software Engineer
@mariussoutier
Write JavaScript in Scala
What is Scala.js?
Scala.js compiles Scala to JavaScript
object HelloUg extends JSApp {

override def main(): Unit = {

// FP for the win

var res, i = 0

val values = js.Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

while (i < values.length) {

res = res + values(i)

i += 1

}

println(res)

}

}
$c_Lug_HelloUg$.prototype.main__V = (function() {

var res = 0;

var i = 0;

var values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

while ((i < $uI(values["length"]))) {

res = ((res + $uI(values[i])) | 0);

i = ((1 + i) | 0)

};

var x = res;

var this$2 = $m_s_Console$();

var this$3 = this$2.outVar$2;

$as_Ljava_io_PrintStream(this$3.tl$1.get__O()).println__O__V(x)

});
$e["ug"] = ($e["ug"] || {});
$e["ug"]["HelloUg"] = $m_Lug_HelloUg$;
But Why?
• Web development is moving to client-side apps,

JavaScript is the browser’s only language
• JavaScript is beyond broken
• Maintaining a large JavaScript project can be difficult
• Keeping up with JavaScript’s ecosystem is nigh-
impossible

(See JavaScript Drinking Game)
Obligatory JavaScript: The Good Parts
Reference
But that’s enough JavaScript bashing for today
Scala.js to the Rescue
• We all love Scala
• Type-safety, packages, traits, …
• Rich standard library
• Share code between backend and front-end
• Full editor support (same file suffix)
• Access to all JS libs and a lot of the Scala ecosystem
Scala.js’ Future
• Size of compiled JavaScript
• Compile Speed
• JS libraries must be wrapped
• Scala is developed with JVM in mind, some things
just don't work in JS land, some JDK parts missing
• Front-end developers might not want to learn Scala
(slackers!)
Drawbacks
• Scala libraries available for Scala.js
• Scalatags, ScalaCSS
• Shapeless, Scalaz, Cats, Monocle
• Sharing code inside a project via common cross-compiled

sbt sub-project
• Data exchange via JSON
• upickle
• JavaScript doesn't necessarily understand your JSON, 

e.g. Longs aren't that long in JavaScript
Sharing Code & Data
How Does It Work?
.scala
.class
.sjsir
run/test
~fastOptJS
fullOptJS
Google
Closure
-opt.js
-fastopt.js
Rhino / Node.js
Rhino / PhantomJS
No DOM
DOM
-jsdeps.min.js
jsDependencies ++= Seq(

"org.webjars" % "jquery" % "1.10.2" / "jquery.js",

"org.webjars" % "angularjs" % "1.4.8" / "angular.js" dependsOn "jquery.js"

)
Targeting JavaScript
Scala / JVM JavaScript
Byte, Short, Int, Float, Double Number
Unit Undefined
Char, Long Scala classes
Custom Scala classes JavaScript class with @JSExport
NullPointerException,
ArrayIndexOutOfBoundsException,
ClassCastException, StackOverflowError, …
Undefined
Reflection -
String.split JS RegEx is different
Pattern Matching on Byte, Short, Int, Float, Double Determined by runtime value, not type
JavaScript Native Types
JS-native Type Maps to Example JS
js.FunctionN scala.FunctioN val fn: js.Function1[Int, Int] =
(i: Int) => i * 2
var fn = function(i)
{

return i * 2;

};
js.Array[T] Seq[T] js.Array(1, 2, 3) [1, 2, 3]
js.Dictionary[T] mutable.Map[String, T] js.Dictionary("a" -> 1, "b" -> 2) {"a": 1, "b": 2}
js.UndefOr[T] Option[T]
val some: js.UndefOr[Int] = 1

val none: js.UndefOr[Int] =
js.undefined

Option(1).orUndefined
1

undefined


1
js.TupleN TupleN js.Tuple2(42, “Scala UG") [42, "Scala UG"]
Dynamic JS
When interacting with JavaScript libraries, its dynamic

nature can’t always be ignored
val guest = js.Dynamic.literal(

name = "Scala User Group"

)
But we can give it a nicer interface
trait Guest extends js.Object {

val name: String = js.native

}



object Guest {

def apply(name: String): Guest =

js.Dynamic.literal(name = name).asInstanceOf[Guest]

}
import js.Dynamic.{global => g}

g.console.log(guest)



val dom = g.document

val p = dom.createElement("p")

p.innerHTML = s"Hi ${guest.name}!"

dom.getElementById("main").appendChild(p)
Wrapping JS Libs (1)
Read documentation and infer types
Wrapping JS Libs (2)
import scala.scalajs.js

import scala.scalajs.js.annotation.JSName



object Numeral {

def apply(n: Double): Numeral = new Numeral(n)

def apply(): Numeral = new Numeral(0)

}



@JSName("numeral")

@js.native

class Numeral(n: Double) extends js.Object {

def format(formatString: String): String = js.native

def unformat(formatString: String): Double = js.native



def add(number: Double): Double = js.native

def subtract(number: Double): Double = js.native

def multiply(number: Double): Double = js.native

def divide(number: Double): Double = js.native



def value(): Double = js.native



def difference(number: Double): Double = js.native

}
jsDependencies ++= Seq(

"org.webjars" % "numeral-js" % "1.5.3-1" / "numeral.js" minified "min/numeral.min.js"

)
Numeral(totalValue).format("0,0.00")
Manipulating the DOM
libraryDependencies ++= Seq(

"org.scala-js" %%% "scalajs-dom" % "0.8.2"

)
import org.scalajs.dom._

val main = document.getElementById("main")

val p = document.createElement("p")

val text = document.createTextNode("Hi Scala User Group!")

p.appendChild(text)

main.appendChild(p)
AJAX
import org.scalajs.dom.ext.Ajax
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue


val eventualUsers =

Ajax

.get(url = "/users/")

.map { xmlHttp =>

xmlHttp.status match {

case 200 =>

import upickle.default._

read[Seq[User]](xmlHttp.responseText)

case other =>

Seq.empty[User]

}

}
• Export classes to JS for use from other JS code
• Can even publish as NPM module
Using Scala.js from JS
@JSExport

case class ExportedUser(

@(JSExport@field) name: String,

@(JSExport@field) email: String

)
@JSExport

@ScalaJSDefined

class Foo extends js.Object {

val x: Int = 4

def bar(x: Int): Int = x + 1

}
Questions?

More Related Content

PDF
Intro to sbt-web
PDF
Converting a Rails application to Node.js
PDF
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
PDF
Bye bye $GLOBALS['TYPO3_DB']
PDF
Full Stack Scala
PDF
Integrating React.js Into a PHP Application
PPTX
Single Page Apps with Drupal 8
PDF
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Intro to sbt-web
Converting a Rails application to Node.js
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
Bye bye $GLOBALS['TYPO3_DB']
Full Stack Scala
Integrating React.js Into a PHP Application
Single Page Apps with Drupal 8
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング

What's hot (20)

PDF
Django Multi-DB in Anger
PDF
Simpler Core Data with RubyMotion
PDF
Using JavaScript in Drupal
KEY
Core animation
PDF
JQuery In Drupal
KEY
Tools that get you laid
PPTX
MongoDB's New Aggregation framework
PDF
Workload Replay in the Cloud: Secret Weapon for Cloud Migrations
PDF
A real-world Relay application in production - Stefano Masini - Codemotion Am...
PPT
Mssql to mysql - Anton Ivanov
PDF
ERGroupware
PDF
Benchx: An XQuery benchmarking web application
PPTX
SQL and NoSQL Better Together in Alasql
PDF
XQuery in the Cloud
PDF
Understanding backbonejs
PPTX
Alasql JavaScript SQL Database Library: User Manual
PPTX
Alasql fast JavaScript in-memory SQL database
PDF
Cutting Edge Data Processing with PHP & XQuery
PDF
Fast Web Applications Development with Ruby on Rails on Oracle
KEY
HTML5 Hacking - Yahoo! Open Hack Day
Django Multi-DB in Anger
Simpler Core Data with RubyMotion
Using JavaScript in Drupal
Core animation
JQuery In Drupal
Tools that get you laid
MongoDB's New Aggregation framework
Workload Replay in the Cloud: Secret Weapon for Cloud Migrations
A real-world Relay application in production - Stefano Masini - Codemotion Am...
Mssql to mysql - Anton Ivanov
ERGroupware
Benchx: An XQuery benchmarking web application
SQL and NoSQL Better Together in Alasql
XQuery in the Cloud
Understanding backbonejs
Alasql JavaScript SQL Database Library: User Manual
Alasql fast JavaScript in-memory SQL database
Cutting Edge Data Processing with PHP & XQuery
Fast Web Applications Development with Ruby on Rails on Oracle
HTML5 Hacking - Yahoo! Open Hack Day
Ad

Similar to Intro to Scala.js - Scala UG Cologne (20)

PDF
Scala @ TechMeetup Edinburgh
KEY
JavaScript Growing Up
PDF
jQuery-1-Ajax
PDF
&lt;img src="../i/r_14.png" />
PDF
jQuery-1-Ajax
PDF
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
PPTX
JavaScript Basics - GameCraft Training
PDF
Scala js (kyiv js 30-01)
PPT
Jet presentation
PDF
An introduction to Scala.js
PDF
Scala.js - yet another what..?
KEY
[Coscup 2012] JavascriptMVC
PPTX
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
PPTX
Scala.js
PPTX
Javascript first-class citizenery
PDF
Scala for Java Programmers
PDF
Scala and jvm_languages_praveen_technologist
PPTX
Scala Italy 2015 - Hands On ScalaJS
PPTX
Alberto Paro - Hands on Scala.js
PDF
Node.js - async for the rest of us.
Scala @ TechMeetup Edinburgh
JavaScript Growing Up
jQuery-1-Ajax
&lt;img src="../i/r_14.png" />
jQuery-1-Ajax
Microsoft PowerPoint - &lt;b>jQuery&lt;/b>-1-Ajax.pptx
JavaScript Basics - GameCraft Training
Scala js (kyiv js 30-01)
Jet presentation
An introduction to Scala.js
Scala.js - yet another what..?
[Coscup 2012] JavascriptMVC
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Scala.js
Javascript first-class citizenery
Scala for Java Programmers
Scala and jvm_languages_praveen_technologist
Scala Italy 2015 - Hands On ScalaJS
Alberto Paro - Hands on Scala.js
Node.js - async for the rest of us.
Ad

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
KodekX | Application Modernization Development
PDF
Encapsulation theory and applications.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPT
Teaching material agriculture food technology
PPTX
Cloud computing and distributed systems.
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Approach and Philosophy of On baking technology
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
NewMind AI Weekly Chronicles - August'25 Week I
Building Integrated photovoltaic BIPV_UPV.pdf
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Network Security Unit 5.pdf for BCA BBA.
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
Encapsulation theory and applications.pdf
Encapsulation_ Review paper, used for researhc scholars
CIFDAQ's Market Insight: SEC Turns Pro Crypto
20250228 LYD VKU AI Blended-Learning.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Teaching material agriculture food technology
Cloud computing and distributed systems.
Advanced methodologies resolving dimensionality complications for autism neur...
Per capita expenditure prediction using model stacking based on satellite ima...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Approach and Philosophy of On baking technology
The AUB Centre for AI in Media Proposal.docx
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Empathic Computing: Creating Shared Understanding
NewMind AI Weekly Chronicles - August'25 Week I

Intro to Scala.js - Scala UG Cologne

  • 1. Intro to Scala.js Marius Soutier Freelance Software Engineer @mariussoutier Write JavaScript in Scala
  • 2. What is Scala.js? Scala.js compiles Scala to JavaScript object HelloUg extends JSApp {
 override def main(): Unit = {
 // FP for the win
 var res, i = 0
 val values = js.Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 while (i < values.length) {
 res = res + values(i)
 i += 1
 }
 println(res)
 }
 } $c_Lug_HelloUg$.prototype.main__V = (function() {
 var res = 0;
 var i = 0;
 var values = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
 while ((i < $uI(values["length"]))) {
 res = ((res + $uI(values[i])) | 0);
 i = ((1 + i) | 0)
 };
 var x = res;
 var this$2 = $m_s_Console$();
 var this$3 = this$2.outVar$2;
 $as_Ljava_io_PrintStream(this$3.tl$1.get__O()).println__O__V(x)
 }); $e["ug"] = ($e["ug"] || {}); $e["ug"]["HelloUg"] = $m_Lug_HelloUg$;
  • 3. But Why? • Web development is moving to client-side apps,
 JavaScript is the browser’s only language • JavaScript is beyond broken • Maintaining a large JavaScript project can be difficult • Keeping up with JavaScript’s ecosystem is nigh- impossible
 (See JavaScript Drinking Game)
  • 4. Obligatory JavaScript: The Good Parts Reference But that’s enough JavaScript bashing for today
  • 5. Scala.js to the Rescue • We all love Scala • Type-safety, packages, traits, … • Rich standard library • Share code between backend and front-end • Full editor support (same file suffix) • Access to all JS libs and a lot of the Scala ecosystem
  • 7. • Size of compiled JavaScript • Compile Speed • JS libraries must be wrapped • Scala is developed with JVM in mind, some things just don't work in JS land, some JDK parts missing • Front-end developers might not want to learn Scala (slackers!) Drawbacks
  • 8. • Scala libraries available for Scala.js • Scalatags, ScalaCSS • Shapeless, Scalaz, Cats, Monocle • Sharing code inside a project via common cross-compiled
 sbt sub-project • Data exchange via JSON • upickle • JavaScript doesn't necessarily understand your JSON, 
 e.g. Longs aren't that long in JavaScript Sharing Code & Data
  • 9. How Does It Work? .scala .class .sjsir run/test ~fastOptJS fullOptJS Google Closure -opt.js -fastopt.js Rhino / Node.js Rhino / PhantomJS No DOM DOM -jsdeps.min.js jsDependencies ++= Seq(
 "org.webjars" % "jquery" % "1.10.2" / "jquery.js",
 "org.webjars" % "angularjs" % "1.4.8" / "angular.js" dependsOn "jquery.js"
 )
  • 10. Targeting JavaScript Scala / JVM JavaScript Byte, Short, Int, Float, Double Number Unit Undefined Char, Long Scala classes Custom Scala classes JavaScript class with @JSExport NullPointerException, ArrayIndexOutOfBoundsException, ClassCastException, StackOverflowError, … Undefined Reflection - String.split JS RegEx is different Pattern Matching on Byte, Short, Int, Float, Double Determined by runtime value, not type
  • 11. JavaScript Native Types JS-native Type Maps to Example JS js.FunctionN scala.FunctioN val fn: js.Function1[Int, Int] = (i: Int) => i * 2 var fn = function(i) {
 return i * 2;
 }; js.Array[T] Seq[T] js.Array(1, 2, 3) [1, 2, 3] js.Dictionary[T] mutable.Map[String, T] js.Dictionary("a" -> 1, "b" -> 2) {"a": 1, "b": 2} js.UndefOr[T] Option[T] val some: js.UndefOr[Int] = 1
 val none: js.UndefOr[Int] = js.undefined
 Option(1).orUndefined 1
 undefined 
 1 js.TupleN TupleN js.Tuple2(42, “Scala UG") [42, "Scala UG"]
  • 12. Dynamic JS When interacting with JavaScript libraries, its dynamic
 nature can’t always be ignored val guest = js.Dynamic.literal(
 name = "Scala User Group"
 ) But we can give it a nicer interface trait Guest extends js.Object {
 val name: String = js.native
 }
 
 object Guest {
 def apply(name: String): Guest =
 js.Dynamic.literal(name = name).asInstanceOf[Guest]
 } import js.Dynamic.{global => g}
 g.console.log(guest)
 
 val dom = g.document
 val p = dom.createElement("p")
 p.innerHTML = s"Hi ${guest.name}!"
 dom.getElementById("main").appendChild(p)
  • 13. Wrapping JS Libs (1) Read documentation and infer types
  • 14. Wrapping JS Libs (2) import scala.scalajs.js
 import scala.scalajs.js.annotation.JSName
 
 object Numeral {
 def apply(n: Double): Numeral = new Numeral(n)
 def apply(): Numeral = new Numeral(0)
 }
 
 @JSName("numeral")
 @js.native
 class Numeral(n: Double) extends js.Object {
 def format(formatString: String): String = js.native
 def unformat(formatString: String): Double = js.native
 
 def add(number: Double): Double = js.native
 def subtract(number: Double): Double = js.native
 def multiply(number: Double): Double = js.native
 def divide(number: Double): Double = js.native
 
 def value(): Double = js.native
 
 def difference(number: Double): Double = js.native
 } jsDependencies ++= Seq(
 "org.webjars" % "numeral-js" % "1.5.3-1" / "numeral.js" minified "min/numeral.min.js"
 ) Numeral(totalValue).format("0,0.00")
  • 15. Manipulating the DOM libraryDependencies ++= Seq(
 "org.scala-js" %%% "scalajs-dom" % "0.8.2"
 ) import org.scalajs.dom._
 val main = document.getElementById("main")
 val p = document.createElement("p")
 val text = document.createTextNode("Hi Scala User Group!")
 p.appendChild(text)
 main.appendChild(p)
  • 16. AJAX import org.scalajs.dom.ext.Ajax import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue 
 val eventualUsers =
 Ajax
 .get(url = "/users/")
 .map { xmlHttp =>
 xmlHttp.status match {
 case 200 =>
 import upickle.default._
 read[Seq[User]](xmlHttp.responseText)
 case other =>
 Seq.empty[User]
 }
 }
  • 17. • Export classes to JS for use from other JS code • Can even publish as NPM module Using Scala.js from JS @JSExport
 case class ExportedUser(
 @(JSExport@field) name: String,
 @(JSExport@field) email: String
 ) @JSExport
 @ScalaJSDefined
 class Foo extends js.Object {
 val x: Int = 4
 def bar(x: Int): Int = x + 1
 }