let and run
Operate on objects.
let and run is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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
itorthis. - 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:
letusesit(a parameter).runusesthis(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:
letexposes the object asitand returns the lambda result, ideal for null-safe transforms.runexposes the object asthisand returns the lambda result, ideal for grouping member access.
Next you will configure objects with apply and add side effects with also.
Frequently asked questions
Is the “let and run” lesson free?
Yes — the full text of “let and run” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
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
- let and run
- apply and also
- with
- Choosing the Right One