skip to content

What is the basic brace syntax of a Kotlin lambda, and what does the implicit `it` refer to?

level: juniorimportance: must knowfreq 85%

answer

  1. { params -> body }, arrow separates params from body
  2. it = auto-name for the single, undeclared parameter
  3. it is NOT a keyword and NOT this
  4. no it for zero or 2+ params
  5. name it explicitly when nested or unclear

basics

~20 s

A lambda is code in curly braces: { x -> body }. When it takes exactly one parameter you can skip naming it and use the built-in name it instead, like list.map { it * 2 }.

solid answer

~40 s

A Kotlin lambda literal is written `{ params -> body }`. Parameters come before the `->` arrow; the body is everything after it. When the lambda has exactly one parameter and you don't declare it explicitly, Kotlin generates the implicit name `it` for that parameter — so `list.map { it * 2 }` is shorthand for `list.map { x -> x * 2 }`. `it` only exists when there is a single parameter and no explicit parameter list; with zero or two-plus parameters it is unavailable and you must name them. The last expression of the body is the lambda's return value. `it` is purely a naming convenience provided by the compiler, not a special object or keyword like `this`.

code

kotlin · 6 lines
kotlin
// Explicit parameter vs implicit it — equivalent
val a = listOf(1, 2, 3).map { n -> n * 2 } // [2, 4, 6]
val b = listOf(1, 2, 3).map { it * 2 }     // [2, 4, 6]

// Two params: it is NOT available, must name them
val sums = listOf(1 to 2, 3 to 4).map { (x, y) -> x + y }

go deeper

for a junior

Can write { it * 2 } and explain that it is the single parameter and -> separates params from the body.

for a middle

Knows it is compiler-generated sugar available only for one undeclared parameter, not a keyword, and equals naming the param explicitly.

for a senior

Articulates readability trade-offs: when to drop it, the nested-lambda shadowing trap, and that the last expression is the result.

for a principal

Frames it as a naming-convention decision in API/style guidelines and weighs it against explicit naming for maintainable codebases.

## What a lambda literal is A **lambda** in Kotlin is an anonymous function written inline using curly braces. Its general form is: ```kotlin { parameterList -> body } ``` - **parameterList**: zero or more comma-separated parameter names, optionally typed. - **`->`**: the arrow that separates parameters from the body. If there are no parameters, you omit both the params and the arrow: `{ doSomething() }`. - **body**: one or more statements; the value of the **last expression** becomes the lambda's result. Example with an explicit parameter: ```kotlin val doubled = listOf(1, 2, 3).map { n -> n * 2 } // [2, 4, 6] ``` ## The implicit `it` When a lambda has **exactly one parameter** and you do **not** declare a parameter list (no `name ->`), the compiler auto-generates a parameter named **`it`**: ```kotlin val doubled = listOf(1, 2, 3).map { it * 2 } // it is each element ``` Key facts about `it`: - It is a normal parameter name supplied for convenience — **not** a keyword and **not** the same as `this`. - It is available **only** for single-parameter lambdas with no explicit arrow. The moment you write `n ->`, `it` is no longer in scope; you use `n`. - For **zero-parameter** lambdas there is no `it`. For **two-or-more** parameters you must name them: `{ a, b -> a + b }` — there is no `it`. - You can always replace `it` with an explicit name. Doing so is recommended when the lambda is long, nested, or the meaning is unclear. ## When NOT to use `it` In **nested** lambdas both default to `it`, which shadows the outer one and is confusing: ```kotlin rows.forEach { row -> row.cells.forEach { cell -> render(row, cell) } } // name them ``` ## Summary `{ x -> ... }` is the full form; `it` is the auto-named single parameter you get when you omit the parameter list. It is sugar, available only for one-parameter lambdas.

  • Is `it` a reserved keyword in Kotlin?
    No. It is an ordinary identifier the compiler supplies for an unnamed single lambda parameter. You can even declare your own variable named `it`, though it's discouraged because it shadows the implicit one.
  • Can you use `it` in a lambda that takes two parameters?
    No. `it` only exists for a single, undeclared parameter. With two or more parameters you must write an explicit list like `{ a, b -> ... }`.

it is like saying 'the thing' when there's only one thing in the room — unambiguous, so you don't bother naming it.

saying these in an interview costs you the question

  • Calling `it` a keyword like `this` or `super`
  • Thinking `it` works for multi-parameter lambdas
  • Believing `it` is a special object rather than just a parameter name
  • Not knowing the `->` separates params from body
  • Claiming the lambda returns its first expression instead of its last

context