Scope Functions (let, run, apply, also, with)
Kotlin is a modern, concise language whose five scope functions — let , run , apply , also , and with — let you operate on an object inside a temporary scope for cleaner, more expressive code.
Learn Scope Functions (let, run, apply, also, with) in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice…
Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll know which scope function to reach for, based on how it references the object and what it returns.
What You'll Learn in This Lesson
1️⃣ let and run
let exposes the object as it and returns the lambda's result — perfect for null-safe transforms. run exposes it as this and also returns the result — handy when you access members directly and want a computed value back.
The maybe?.let { } pattern runs the block only when the value is present — a clean replacement for a null check.
2️⃣ apply, also, and with
apply ( this receiver) returns the object — ideal for configuration. also ( it receiver) also returns the object — ideal for side effects like logging. with takes the object as an argument and returns the lambda result.
Because apply and also return the object, they chain beautifully — set up, peek, and keep going in one fluent expression.
Your turn. Replace the TODO , then run and compare.
Mini-Challenge: Build and Describe a User
Use apply to construct, with to describe, and let for a nullable.
Common Errors (and the fix)
- Returning the wrong thing — apply / also return the object; let / run / with return the lambda result. Pick the right one.
- Mixing up it and this — let / also use it ; run / with / apply use this .
- Over-nesting scope functions — Deeply nested let { } blocks get unreadable. Name parameters or extract a function.
- Using with on a nullable — with isn't a safe call. For nullables, prefer ?.let or ?.run .
Pro Tips
- 💡 Config? apply. Set several properties on a fresh object and get it back.
- 💡 Null + transform? let. value?.let { ... } is the cleanest guarded transform.
- 💡 Peek mid-chain? also. Log or validate without breaking the fluent flow.
📋 Quick Reference — Scope Functions
Function
Receiver
Returns
let
it
lambda result
run
this
with
this (arg)
apply
the object
also
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ let / run / with return the lambda result
- ✅ apply / also return the object — great for chaining
- ✅ let / also use it ; run / with / apply use this
- ✅ ?.let guards nullables cleanly
- ✅ Choose by receiver and return value to express intent
- ✅ Next lesson: Coroutines Intro
Practice quiz
How many scope functions does Kotlin's standard library provide?
- Three
- Five: let, run, with, apply, also
- Two: let and run
- Seven
Answer: Five: let, run, with, apply, also. The five scope functions are let, run, with, apply, and also.
Which two axes distinguish the scope functions?
- Speed and memory
- How the object is referenced (it/this) and what is returned
- Nullability and mutability
- Whether they are inline or not
Answer: How the object is referenced (it/this) and what is returned. They differ by receiver reference (it vs this) and return value (lambda result vs object).
What does apply return?
- The lambda result
- Nothing (Unit)
- The object itself
- A nullable copy
Answer: The object itself. apply returns the receiver object, making it ideal for configuration.
Which scope function uses it as the receiver and returns the lambda result?
- let
- apply
- run
- with
Answer: let. let exposes the object as it and returns the lambda's result.
What does run expose the object as, and what does it return?
- it; the object
- this; the lambda result
- it; the lambda result
- this; the object
Answer: this; the lambda result. run uses this as the receiver and returns the lambda result.
Which is the key difference between apply and also?
- apply returns the object, also returns the result
- apply uses this, also uses it (both return the object)
- Only also can be chained
- apply is for nullables only
Answer: apply uses this, also uses it (both return the object). Both return the object; apply uses this (config), also uses it (side effects).
What is the cleanest way to run a block only when a nullable value is non-null?
- with(value) { }
- value.run { }
- value?.let { }
- value!!.apply { }
Answer: value?.let { }. value?.let { } combines a safe call with let so the block runs only for a present value.
How is with different from the other four scope functions?
- It is not an extension; it takes the object as an argument
- It returns the object
- It uses it as the receiver
- It cannot return a value
Answer: It is not an extension; it takes the object as an argument. with is a regular function taking the object as an argument; it uses this and returns the lambda result.
Which scope function is best for object configuration that returns the configured object?
- let
- with
- apply
- also
Answer: apply. apply uses this and returns the object, perfect for setting several properties.
Which functions expose the object as it (rather than this)?
- run and with
- let and also
- apply and run
- with and apply
Answer: let and also. let and also use it; run, with, and apply use this.
Continue this course
- Previous: Extension Functions
- Next: Coroutines Intro