Kotlin from JavaScript perspective
#30DaysOfKotlin learning program
--
The first two weeks of #30DaysOfKotlin have passed and, I am sharing below some basic takeaways from Kotlin from a javaScript developer perspective.
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.
PFB a classic hello world function:
fun helloKotlinWorld() {
println("Hello Kotlin world!")
}
helloKotlinWorld()// Hello Kotlin world!
Note the syntax of the function is almost similar, with the keyword function
shortened to fun
. More on functions later.
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:
You must use double quotes to create those strings, the single quote should be used if you are creating a character literal. This is similar to other programming languages like C, Java, etc.
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 the
const
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"
With a list or array defined with val
, you can't reassign, but you can still mutate the contents of the list/array.
// 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
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;
println(java.util.Arrays.toString(concatenatedArr))// 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 -> Javavar lang = "Kotlin"
for (c in lang) {
println(c)
}// result below:
K
o
t
l
i
n
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 kotlinrepeat(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;
isEven(9)// result
res0: kotlin.Boolean = false/// JAVASCRIPT single line arrow functions
const isEven = (num) => num % 2 == 0;
isEven(9)// result: false
Overall, it was very interesting navigating through the codelabs and correlating things with the language I love the most and this really enhances the learning experience.
While playing in Kotlin's playground, I have come to this conclusion that exploring a new language is full of fun, and sometimes it might not seem easy to shift the mindset from one perspective since we are used to code in a particular manner.
But, while there are will always be new languages and frameworks coming into demand with each passing year, the main thing that matters is the logical understanding and coding for problem-solving. In my view, the syntax is not the hard part of learning a new language but the implementation details and how easily can we solve a problem using it.
That’s all for now, I hope you are all having fun exploring Kotlin. Keep learning and stay safe everyone :)