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).

Scala.js exhibits both means to call JavaScript APIs from Scala.js, and to be called from JavaScript code.

Type Correspondance

Some Scala types are directly mapped to corresponding underlying JavaScript types. These correspondances can be used when calling Scala.js code from JavaScript and when defining typed interfaces for JavaScript code.

Scala typeJavaScript typeRestrictions
scala.Bytenumberinteger, range (-128, 127)
scala.Shortnumberinteger, range (-32768, 32767)
scala.Intnumberinteger, range (-2147483648, 2147483647)
subtypes of js.Objectcorresponding JavaScript typesee calling JavaScript guide
other Scala classes
including value classes
opaque, except for exported methods
Note: toString() is always exported
see exporting Scala.js APIs to JavaScript

On the other hand, some JavaScript (collection) types have similar types in Scala. Instead of mapping them directly, Scala.js provides conversions between them. We show with a couple of snippets how you can convert from JavaScript to Scala types and back. Please refer to the Scaladocs for details.

js.Array[T] <--> mutable.Seq[T]

import scala.scalajs.js

val jsArr = js.Array(1, 2, 3)

// Scala style operations on js.Array (returns a js.Array)
val x: js.Array[Int] = jsArr.takeWhile(_ < 3)

// Use a js.Array as a Scala mutable.Seq
val y: mutable.Seq[Int] = jsArr

// toArray (from js.ArrayOps) -- Copy into scala.Array
val z: scala.Array[Int] = jsArr.toArray

import js.JSConverters._

val scSeq = Seq(1, 2, 3)

// Seq to js.Array -- Copy to js.Array
val jsArray: js.Array[Int] = scSeq.toJSArray

js.Dictionary[T] <--> mutable.Map[String, T]

import scala.scalajs.js

val jsDict = js.Dictionary("a" -> 1, "b" -> 2)

// Scala style operations on js.Dictionary (returns mutable.Map)
val x: mutable.Map[String, Int] = jsDict.mapValues(_ * 2)

// Use a js.Dictionary as Scala mutable.Map
val y: mutable.Map[String, Int] = jsDict

import js.JSConverters._

val scMap = Map("a" -> 1, "b" -> 2)

// Map to js.Dictionary -- Copy to js.Dictionary
val jsDictionary: js.Dictionary[Int] = scMap.toJSDictionary

js.UndefOr[T] <--> Option[T]

import scala.scalajs.js

val jsUndefOr: js.UndefOr[Int] = 1

// Convert to scala.Option
val x: Option[Int] = jsUndefOr.toOption

import js.JSConverters._

val opt = Some(1)

// Convert to js.Undefined
val y: js.UndefOr[Int] = opt.orUndefined