Kotlin from JavaScript perspective

#30DaysOfKotlin learning program

1) Semicolons are optional

just like JavaScript. Although personally, I am not a fan of this optional semicolon feature, but if you are used to skipping those semicolons Kotlin has a warm welcome message right there for you.

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

2) Strings must be double-quoted

In JavaScript, we have the freedom to create strings with either double or single quotes. I personally prefer single quotes for consistency. And this lead to errors while writing the first hello world program for me. Proof below:

3) Strongly typed

One needs to explicitly typecast whenever required.

4) Variable declaration

Two types of variables are supported by Kotlin — changeable and unchangeable.

  • var — It can be both assigned and changed whenever you want.
  • val — It is similar to theconst in JavaScript. We can assign value only once. If you try to assign something again, you get an error. However, the value can be changed
// 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");
[React, Vue, jQuery, Node]

5) `when` statement

The when statement is similar to switch in JavaScript, but it automatically breaks at the end of each branch. Conditions in a when statement can use ranges, too.

var day = 0;

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

6) array concatenation using + operator

Using + operator for concatenation is very intuitive and while it is used only for strings in JavaScript, Kotlin extends this to concat arrays as well. Cool feature, I must say :)

val arr1 = intArrayOf(1,2,3);
val arr2 = intArrayOf(4,5,6);
val concatenatedArr = arr1 + arr2;
// result: [4, 5, 6, 1, 2, 3]

7) array loops

The for loop is used to iterate through ranges, arrays, etc.and if you need to access index, call the withIndex method on the array.

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) {
// result below:

8) Repeat

Kotlin also has repeat loops that execute the code for a given number of times.

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

9) Compact functions,

or single-expression functions, are a pattern in Kotlin which look quite similar to the single line arrow functions in javaScript with some syntactical differences.

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



Web Developer, Accessibility Advocate, Google Developers Expert, MDE @Cloudinary. https://.anuradhakumari.com/ ‘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
Anuradha Kumari

Web Developer, Accessibility Advocate, Google Developers Expert, MDE @Cloudinary. https://.anuradhakumari.com/ ‘Choose RIGHT over easy, always!’