Interoperability

A key feature of Scala.js is its interoperability with JavaScript code, which far exceeds that of many other languages targeting JavaScript. Except of course for languages that translate almost literally to JavaScript (e.g., TypeScript and CoffeeScript).

See for yourself: on the right, Scala.js natively talks to the DOM API, and we can hardly notice the border between the two languages.

ES6
var xhr = new XMLHttpRequest();

xhr.open("GET",
  "https://api.twitter.com/1.1/search/" +
  "tweets.json?q=%23scalajs"
);
xhr.onload = (e) => {
  if (xhr.status === 200) {
    var r = JSON.parse(xhr.responseText);
    $("#tweets").html(parseTweets(r));
  }
}
Scala.js
val xhr = new XMLHttpRequest()

xhr.open("GET",
  "https://api.twitter.com/1.1/search/" +
  "tweets.json?q=%23scalajs"
)
xhr.onload = { (e: Event) =>
  if (xhr.status == 200) {
    val r = JSON.parse(xhr.responseText)
    $("#tweets").html(parseTweets(r))
  }
}

The following sections explain all the details: