Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

JS function

You can inline JavaScript code into your Kotlin code using the js("…") function. This should be used with extreme care.

fun main() {
    js("alert(\"alert from Kotlin!\")") // 1
}
  1. Sending a JavaScript alert from a Kotlin function.
fun main(){
  val json = js("{}")               // 1
  json.name = "Jane"                // 2
  json.hobby = "movies"
  println(JSON.stringify(json))     // 3
}
  1. Creates a JavaScript object literal. The js(...) function return type is dynamic.
  2. Adds some properties by utilizing the dynamic type capabilities.
  3. Passes the JSON to JavaScript API.