Search⌘ K

Kotlin Keyword to Reference Single Parameter

Learn about an important keyword and how to use it in Kotlin.

The keyword it

In Kotlin, the keyword it is a shorthand notation that allows us to reference a single parameter in a lambda or anonymous function, thereby making our code more concise and readable. We typically use it when we have a lambda with only one parameter. Instead of explicitly naming the parameter, we can use it to reference it. Here’s an example:

Kotlin
fun main(){
val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val oddNumbers = numbers.filter { it % 2 != 0 }
println(oddNumbers)
}

In the code ...