Kotlin for JavaScript

Unified coding model. Kotlin is an interesting programming language that can write codes for both web front-end JavaScript and back-end server side JavaScript as well as Java Virtual Machine (JVM). Kotlin can work things out with existing JavaScript and JVM ecosystems.

In this article, I would like to teach myself on how to target JavaScript with Kotlin.

Official Kotlin documents on the Web: here

Official document recommends to use Gradle build tool. So I will use Gradle to generate JavaScript code from Kotlin code.

Also I would like to use jQuery as an example of existing JavaScript library.

Here is a sample HTML with JavaScript

<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("p").click(function(){
    $(this).text("Bless You!");
  });
});
</script>
</head>
<body>

<p>If you click on me, You will be blessed.</p>

</body>
</html>

The JavaScript part of the example above is:

$(document).ready(function(){
  $("p").click(function(){
    $(this).text("Bless You!");
  });
});

I will produce above JavaScript code with Kotlin. Follow me next.

Relevant source codes are available at GitHub.

Source Code at GitHub
Source Code at GitHub