← Kotlin tutorial

Kotlin

Variables & Data Types

val declares a value that can't be reassigned; var declares one that can.

Example: preferring val, using var only when needed

fun main() {
    val name = "Ada"
    var age = 21
    println("$name is $age years old.")
}
Ada is 21 years old.

Prefer val by default — the exact same recommendation the JavaScript tutorial gives for const over let, for the same reason: it prevents an accidental reassignment from compiling at all, catching a mistake immediately instead of it silently happening somewhere unexpected. Kotlin usually infers the type from the assigned value, though you can write it explicitly: val age: Int = 21.

Example
fun main() {
    val name = "Ada"
    var age = 21
    println("$name is $age years old.")
}
Output
Ada is 21 years old.