Kotlin Academy · Lesson

let and run

Operate on objects.

Lesson 1 of 413 steps

let and run is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

What Are Scope Functions?

Kotlin offers five scope functions: let, run, with, apply, and also. They let you execute a block of code on an object inside a temporary scope.

  • You refer to the object as it or this.
  • The block returns either the object itself or the lambda result.

In this lesson we focus on let and run.

The let Function

let executes a block where the object is available as it, and returns the lambda result.

It is great for running code only when a value is non-null, and for transforming a value into something else.

fun main() {
    val name = "Alice"
    val length = name.let {
        println("Name is $it")
        it.length
    }
    println("Length: $length")
}

let with Nullable Values

The most common use of let is with the safe-call operator ?. The block runs only when the value is not null.

fun main() {
    val name: String? = "Bob"
    name?.let {
        println("Hello, $it")
    }
    val empty: String? = null
    empty?.let {
        println("This never prints")
    }
    println("Done")
}

Renaming it in let

Inside let you can rename it to a meaningful name by declaring a lambda parameter.

fun main() {
    val price = 100
    price.let { amount ->
        println("The amount is $amount")
    }
}

The run Function

run executes a block where the object is available as this, and returns the lambda result.

Use it when you want to access members directly without a prefix.

fun main() {
    val text = "Kotlin"
    val upper = text.run {
        println("Length is $length")
        uppercase()
    }
    println(upper)
}

run Without a Receiver

run can also be called without an object. It simply runs a block and returns its result. This is useful for grouping statements and computing a value.

fun main() {
    val result = run {
        val a = 5
        val b = 10
        a + b
    }
    println("Result: $result")
}

it vs this

The key difference between let and run is how they reference the object:

  • let uses it (a parameter).
  • run uses this (a receiver, often implicit).

Both return the result of the last expression in the block.

fun main() {
    val word = "hi"
    val a = word.let { it.length }
    val b = word.run { length }
    println("$a $b")
}

Chaining with let

Because let returns the lambda result, you can chain calls and transform a value step by step.

fun main() {
    val number = 4
    val output = number
        .let { it * 2 }
        .let { it + 1 }
    println(output)
}

run for Null Checks and Computation

Like let, run works with safe calls. It combines a null check with member access in one block.

fun main() {
    val message: String? = "Welcome"
    val length = message?.run {
        println(this)
        length
    }
    println("Length: $length")
}

Returning Different Types

Both functions can return a type different from the object. The return type is whatever the last line evaluates to.

fun main() {
    val n = 10
    val label: String = n.let { "Value is $it" }
    println(label)
}

When to Reach for let or run

Use let for null-safe operations and value transformations where it reads clearly.

Use run when you want to call several members of the object and compute a result, with this giving direct access.

Quick Check

Test your understanding of let and run.

Recap

You learned the two result-returning scope functions:

  • let exposes the object as it and returns the lambda result, ideal for null-safe transforms.
  • run exposes the object as this and returns the lambda result, ideal for grouping member access.

Next you will configure objects with apply and add side effects with also.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “let and run” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “let and run”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

What will I learn in “let and run”?

Operate on objects. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “let and run” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. let and run
  2. apply and also
  3. with
  4. Choosing the Right One
← Back to Kotlin Academy