A key feature of Scala.js is its interoperability with JavaScript code, whichfar exceeds that of many other languages targeting JavaScript. Except of coursefor languages that translate almost literally to JavaScript (e.g.,TypeScript andCoffeeScript).

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

ES6

  1. var xhr = new XMLHttpRequest();
  2. xhr.open("GET",
  3. "https://api.twitter.com/1.1/search/" +
  4. "tweets.json?q=%23scalajs"
  5. );
  6. xhr.onload = (e) => {
  7. if (xhr.status === 200) {
  8. var r = JSON.parse(xhr.responseText);
  9. $("#tweets").html(parseTweets(r));
  10. }
  11. };
  12. xhr.send();

Scala.js

  1. val xhr = new XMLHttpRequest()
  2. xhr.open("GET",
  3. "https://api.twitter.com/1.1/search/" +
  4. "tweets.json?q=%23scalajs"
  5. )
  6. xhr.onload = { (e: Event) =>
  7. if (xhr.status == 200) {
  8. val r = JSON.parse(xhr.responseText)
  9. $("#tweets").html(parseTweets(r))
  10. }
  11. }
  12. xhr.send()

The following sections explain all the details: