Kotlin Help

Kotlin for JavaScript

Kotlin/JS provides the ability to transpile your Kotlin code, the Kotlin standard library, and any compatible dependencies to JavaScript. The current implementation of Kotlin/JS targets ES5.

The recommended way to use Kotlin/JS is via the kotlin.js and kotlin.multiplatform Gradle plugins. They provide a central and convenient way to set up and control Kotlin projects targeting JavaScript. This includes essential functionality such as controlling the bundling of your application, adding JavaScript dependencies directly from npm, and more. To get an overview of the available options, check out the Kotlin/JS project setup documentation.

Use cases for Kotlin/JS

There are numerous ways that Kotlin/JS can be used. To provide you some inspiration, here's a non-exhaustive list of scenarios in which you can use Kotlin/JS.

  • Write frontend web applications using Kotlin/JS

    • Kotlin/JS allows you to leverage powerful browser and web APIs in a type-safe fashion. Create, modify and interact with elements in the Document Object Model (DOM), use Kotlin code to control the rendering of canvas or WebGL components, and enjoy access to many more of the features supported in modern browsers.

    • Write full, type-safe React applications with Kotlin/JS using the kotlin-wrappers provided by JetBrains, which provide convenient abstractions and deep integrations for one of the most popular JavaScript frameworks. kotlin-wrappers also provides support for a select number of adjacent technologies like react-redux, react-router, or styled-components. Interoperability with the JavaScript ecosystem also means that you can also use third-party React components and component libraries.

    • Use the Kotlin/JS frameworks that take full advantage of Kotlin concepts, its expressive power and conciseness.

  • Write server-side and serverless applications using Kotlin/JS

    • The Node.js target provided by Kotlin/JS enables you to create applications that run on a server or get executed on serverless infrastructure. You benefit from the same advantages as other applications executing in a JavaScript runtime, such as faster startup speed and a reduced memory footprint. With kotlinx-nodejs, you have typesafe access to the Node.js API directly from your Kotlin code.

  • Use Kotlin's multiplatform projects to share code with other Kotlin targets

    • All Kotlin/JS functionality can also be accessed when using the Kotlin multiplatform Gradle plugin.

    • If you have a backend written in Kotlin, you can share common code such as data models or validation logic with a frontend written in Kotlin/JS, allowing you to write and maintain full-stack web applications.

    • You could also share business logic between your web interface and mobile apps for Android and iOS, and avoid duplicating commonly used functionality like providing abstractions around REST API endpoints, user authentication, or your domain models.

  • Create libraries for use with JavaScript and TypeScript

    • You don't have to write your whole application in Kotlin/JS, either – you can also generate libraries from your Kotlin code that can be consumed as modules from any code base written in JavaScript or TypeScript, regardless of other frameworks or technologies used. This approach of creating hybrid applications allows you to leverage the competencies that you and your team might already have around web development, while helping you reduce the amount of duplicated work, and making it easier to keep your web target consistent with other targets of your application.

Of course, this is not a complete list of how you can use Kotlin/JS to your advantage, but merely a selection of cherry-picked cases. We invite you to experiment with combinations of these use cases, and find out what works best for your project.

Regardless of your specific use case, Kotlin/JS projects can use compatible libraries from the Kotlin ecosystem, as well as third-party libraries from the JavaScript and TypeScript ecosystems. To use the latter from Kotlin code, you can either provide your own typesafe wrappers, use community-maintained wrappers, or let Dukat automatically generate Kotlin declarations for you. Using the Kotlin/JS-exclusive dynamic type allows you to loosen the constraints of Kotlin's type system, allowing you to skip creating detailed library wrappers - at the expense of type safety.

Kotlin/JS is also compatible with the most common module systems: UMD, CommonJS, and AMD. Being able to produce and consume modules means that you can interact with the JavaScript ecosystem in a structured manner.

Kotlin/JS frameworks

Modern web development benefits significantly from frameworks that simplify building web applications. Here are examples of popular web frameworks for Kotlin/JS written by different authors:

KVision

KVision is an object-oriented web framework that makes it possible to write applications in Kotlin/JS with ready-to-use components that can be used as building blocks for your application’s user interface. You can use both reactive and imperative programming models to build your frontend, use connectors for Ktor, Spring Boot, and other frameworks to integrate it with your server-side applications, and share code using Kotlin Multiplatform.

Visit https://kvision.io for documentation, tutorials, and examples.

For updates and discussions about the framework, join #kvision and #javascript channels in the Kotlin Slack.

fritz2

fritz2 is a standalone framework for building reactive web user interfaces. It provides its own type-safe DSL for building and rendering HTML elements, and it makes use of Kotlin’s coroutines and flows to express components and their data bindings. It provides state management, validation, routing, and more out of the box, and integrates with Kotlin Multiplatform projects.

Visit https://www.fritz2.dev for documentation, tutorials, and examples.

For updates and discussions about the framework, join the #fritz2 and #javascript channels in the Kotlin Slack.

Doodle

Doodle is a vector-based UI framework for Kotlin/JS. Doodle applications use the browser’s graphics capabilities to draw user interfaces instead of relying on DOM, CSS, or Javascript. By using this approach, Doodle gives you precise control over the rendering of arbitrary UI elements, vector shapes, gradients, and custom visualizations.

Visit https://nacular.github.io/doodle/ for documentation, tutorials, and examples.

For updates and discussions about the framework, join #doodle and #javascript channels in the Kotlin Slack.

Compose for Web

Compose for Web, a part of Compose Multiplatform brings Google's Jetpack Compose UI toolkit to your browser. It allows you to build reactive web user interfaces using the concepts introduced by Jetpack Compose. It provides a DOM API to describe your website, as well as an experimental set of multiplatform layout primitives. Compose for Web also gives you the option to share parts of your UI code and logic across Android, desktop, and web.

You can find more information about Compose Multiplatform on its landing page.

Join the #compose-web channel on Kotlin Slack to discuss Compose for Web, or #compose for general Compose Multiplatform discussions.

Kotlin/JS, Today and Tomorrow

Want to know more about Kotlin/JS?

In this video, Kotlin Developer Advocate Sebastian Aigner will explain the main Kotlin/JS benefits to you, share some tips and use cases, and also tell you about the plans and upcoming features for Kotlin/JS.

Get started with Kotlin/JS

If you're new to Kotlin, a good first step is to familiarize yourself with the basic syntax of the language.

To start using Kotlin for JavaScript, please refer to Set up a Kotlin/JS project. You can also pick a hands-on lab to work through or check out the list of Kotlin/JS sample projects for inspiration. They contain useful snippets and patterns and can serve as nice jump-off points for your own projects.

Hands-on labs for Kotlin/JS

  • Building Web Applications with React and Kotlin/JS guides you through the process of building a simple web application using the React framework, shows how a type-safe Kotlin DSL for HTML makes it easy to build reactive DOM elements, and illustrates how to use third-party React components and obtain information from APIs, all while writing the whole application logic in pure Kotlin/JS.

  • Building a Full Stack Web App with Kotlin Multiplatform teaches the concepts behind building an application that targets Kotlin/JVM and Kotlin/JS by building a client-server application that makes use of shared code, serialization, and other multiplatform paradigms. It also provides a brief introduction to working with Ktor both as a server- and client-side framework.

Sample projects for Kotlin/JS

  • Full-stack Spring collaborative to-do list shows how to create a to-do list for collaborative work using kotlin-multiplatform with JS and JVM targets, Spring for the backend, Kotlin/JS with React for the frontend, and RSocket.

  • Kotlin/JS and React Redux to-do list implements the React Redux to-do list using JS libraries (react, react-dom, react-router, redux, and react-redux) from npm and Webpack to bundle, minify, and run the project.

  • Full-stack demo application guides you through the process of building an app with a feed containing user-generated posts and comments. All data is stubbed by the fakeJSON and JSON Placeholder services.

New Kotlin/JS IR compiler

The new Kotlin/JS IR compiler (currently with Beta stability) comes with a number of improvements over the current default compiler. For example, it improves the size of generated executables via dead code elimination and makes it smoother to interoperate with the JavaScript ecosystem and its tooling. By generating TypeScript declaration files (d.ts) from Kotlin code, the new compiler makes it easier to create “hybrid” applications that mix TypeScript and Kotlin code, and leverage code-sharing functionality using Kotlin Multiplatform.

To learn more about the available features in the new Kotlin/JS IR compiler and how to try it for your project, visit the Kotlin/JS IR compiler documentation page and the migration guide.

Join the Kotlin/JS community

You can also join #javascript channel in the official Kotlin Slack and chat with the community and the team.

Last modified: 03 December 2021