Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Kotlin
Kotlinmedium

Scope functions (`let`, `apply`, `also`, `run`, `with`) — how do they differ?

Tags
#scope-functions#let#apply#also
Back to categoryPractice quiz

Answer

They mainly differ by the receiver (`this` vs `it`) and the return value (receiver vs lambda result). Example: `apply` configures and returns the object; `let` transforms and returns the lambda result.

val user = User("Ada")
  .apply { active = true } // returns receiver

val nameLen = user.name.let { it.length } // returns lambda result

Advanced answer

Deep dive

Scope functions answer two questions:

  • Do you want the context object as `this` (receiver) or as `it` (argument)?
  • Do you want to return the object or the lambda result?

Quick cheatsheet:

  • `let`: `it`, returns lambda result (great with null-safe calls).
  • `apply`: `this`, returns receiver (configure/build object).
  • `also`: `it`, returns receiver (side effects like logging).
  • `run`: `this`, returns lambda result (compute using receiver).
  • `with(x)`: like `x.run { ... }` but as a function (non-extension).

Common pitfalls

  • Overusing scope functions and hurting readability.
  • Using `apply` for side effects and forgetting it returns the receiver (not the computed value).