Kotlin from JavaScript perspective

#30DaysOfKotlin learning program

1) Semicolons are optional

fun helloKotlinWorld() {
println("Hello Kotlin world!")
}
helloKotlinWorld()
// Hello Kotlin world!

2) Strings must be double-quoted

3) Strongly typed

4) Variable declaration

// var example
var language = "English"
language = "Hindi"
// val exampleval lang = "English"
lang = "Hindi"
error: val cannot be reassigned
lang = "Hindi"
// reassigning a val throws error
val myList = mutableListOf("React", "Vue", "Svelte");
myList = mutableListOf("Node", "Deno");
error: val cannot be reassigned
// changing the content of val works fine
val list = mutableListOf("React", "Vue", "jQuery", "jQuery");
list.remove("jQuery");
list.add("Node")
println(list)
[React, Vue, jQuery, Node]

5) `when` statement

var day = 0;

when (day) {
0 -> println("Sunday!")
in 1..4 -> println("Weekdays")
else -> println("Weekend!!") // default
}
// output: Sunday!

6) array concatenation using + operator

val arr1 = intArrayOf(1,2,3);
val arr2 = intArrayOf(4,5,6);
val concatenatedArr = arr1 + arr2;
println(java.util.Arrays.toString(concatenatedArr))
// result: [4, 5, 6, 1, 2, 3]

7) array loops

val lang = arrayOf("C", "C++", "Java")
for (item in lang) {
print(item + " ")
}
// result: C C++ Javaval lang = arrayOf("C", "C++", "Java")
for ((index, item) in lang.withIndex()) {
println("$index -> $item\n")
}
// result below:0 -> C
1 -> C++
2 -> Java
var lang = "Kotlin"
for (c in lang) {
println(c)
}
// result below:
K
o
t
l
i
n

8) Repeat

repeat(2) {
println("Learning kotlin\n")
}
// result
Learning kotlin
Learning kotlin
repeat(0) {
println("This will not be printed\n")
}

9) Compact functions,

/// KOTLIN single expression functions
fun isEven(num: Int) = num % 2 == 0;
isEven(9)
// result
res0: kotlin.Boolean = false
/// JAVASCRIPT single line arrow functions
const isEven = (num) => num % 2 == 0;
isEven(9)
// result: false

Tech enthusiast, Development Specialist @teammediaocean. Creator of https://explore-a11y.netlify.app/ My motto — ‘Choose RIGHT over easy, always!’

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store