🐦 Kotlin Beginner

What is the it keyword in Kotlin lambdas?

Answer

When a lambda expression has exactly one parameter, you can omit the parameter declaration and its arrow, and refer to the parameter using the implicit name it. For example, list.filter { it > 5 } is shorthand for list.filter { element -> element > 5 }. The it convention keeps short lambdas concise. For nested lambdas or when clarity matters, always declare an explicit parameter name to avoid ambiguity about which object it refers to. If a lambda has zero or two or more parameters, it is not available.