Quick Javascript / Kotlin comparison
This part of the guide might be helpful to map noteworthy knowledge from javascript to kotlin. Detailed kotlin introduction will be proper handled in the next section.
Online playgrounds
Kotlin has one, brand new.
| Kotlin | Javascript |
|---|---|
| play.kotlinlang.org | playcode.io |
| codesandbox.io | |
| stackblitz.com | |
| codepen.io | |
| npm.runkit.com |
Command line interpreters/compilers
Kotlin is general purpose, compiled language. There is a compiler and it's installable through various ways.
For linux, sdkman is the best choice, for mac homebrew.
Javascript can run on command line as well in the form of node.js, and there are several ways to install it.
Online package registries
Kotlin is heavily dependent of jvm and maven registry. There is no equivalent tool to npm or yarn, maven and gradle does project management work but lack a good amount of features. We'll discuss them later.
Notable language features and its equivalents
There will be a more detailed section on it in the next chapter.
Variables
| Kotlin | Javascript |
|---|---|
| // regular variables | |
| var x: Int | var x |
| var x = 10 // type inference | let x |
| // constants | |
| val x: Int = 10 | const x = 10 |
| val x = 10 | |
| // compile-time constants | |
| const x = 10 // checked | // there is no such thing |
| const x = something() // error |
Data structures
| Kotlin | Javascript |
|---|---|
| // lists | |
| val x = listOf(1,2,3) | const x = [1,2,3] |
| val y = setOf(1,2,2,3) | const y = new Set([1,2,3]) |
| // maps | |
| val x = mapOf("a" to 1, "b" to 2) | const x = { a: 1, b: 2 } |
| // classes | |
| class X | class X {} |
| class Y(public val x: String) | class X { constructor(x) { this.x = x} } |
We'll discuss classes in details later.
Destructuring
| Kotlin | Javascript |
|---|---|
| // lists | |
| val (first, second) = listOf(1,2,3) | const [first, second] = [1,2,3] |
| // maps (works with data classes too) | |
| val (first, second) = mapOf("a" to 1, "b" to 2) | const {a, b} = {a: 1, b: 2} |
| const {a:first, b:second} = {a: 1, b: 2} |