Android Academy · Lesson

let, also, and Scope Functions

Work with nullable values.

Lesson 3 of 413 steps

let, also, and Scope Functions is a free Android Academy lesson on CoddyKit. This is lesson 3 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 Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

What Are Scope Functions?

Scope functions let you run a block of code in the context of an object. They make code more concise, especially around nullable values.

This lesson focuses on let and also, the two most useful for null safety, and how they relate to the safe call operator.

The let Function

let runs a block and passes the object in as it. It returns whatever the block returns.

It is handy for transforming a value or scoping a temporary name.

fun main() {
    val length = "Kotlin".let { it.length }
    println(length)  // prints: 6
}

let for Null Safety

Combine ?. with let to execute a block only when a value is not null. The block runs once, with the value as it.

If the value is null, the block is skipped entirely.

fun main() {
    val name: String? = "Ada"
    name?.let {
        println("Hello, " + it)
    }
}

Skipped When Null

When the receiver is null, ?.let does nothing. The block never executes.

This program prints nothing because name is null.

fun main() {
    val name: String? = null
    name?.let {
        println("Hello, " + it)
    }
}

Renaming it

You can give the parameter a clearer name instead of it. This improves readability in longer blocks.

Use an arrow to name it explicitly.

val name: String? = "Bo"
name?.let { value ->
    println(value.uppercase())
}

The also Function

also also passes the object as it, but it returns the original object instead of the block's result.

It is great for side effects like logging while keeping the value unchanged in a chain.

fun main() {
    val n = 5.also { println("got " + it) }
    println(n)  // prints: 5
}

let vs also Return Value

The key difference: let returns the block result, while also returns the original object.

Choose let to transform, and also to peek without changing the value.

val a = "hi".let { it.length }   // a = 2 (Int)
val b = "hi".also { it.length }  // b = "hi" (String)

also for Logging

Because also returns the object, it slots into a chain to inspect a value mid-flow.

Here we log a nullable value only if it is present, then keep using it.

fun main() {
    val token: String? = "abc"
    val result = token?.also { println("token: " + it) }
    println(result)
}

let with Elvis

Pair ?.let with the Elvis operator to provide an alternative when the value is null.

The block produces a result when present; the Elvis side handles the null case.

fun main() {
    val raw: String? = null
    val out = raw?.let { it.trim() } ?: "empty"
    println(out)  // prints: empty
}

Choosing a Scope Function

For null safety, reach for let when you want to transform or use a non-null value in a block. Reach for also when you want a side effect but must keep the original object.

Both pair naturally with the safe call operator to avoid null crashes.

user?.let { save(it) }      // transform/use
user?.also { log(it) }      // side effect, keep user

A Combined Example

This program uses ?.let to build a message and Elvis to fall back. It shows the everyday rhythm of null-safe Kotlin.

No explicit null checks appear, yet it is fully safe.

fun main() {
    val city: String? = "Goreme"
    val label = city?.let { "City: " + it } ?: "Unknown"
    println(label)
}

Quick Check

Test your understanding of let and also.

Recap

Scope functions run a block in an object's context. let passes the object as it and returns the block result; also passes it and returns the original object.

With ?.let you act on a value only when it is not null. Next you will pull these ideas together to avoid NullPointerExceptions entirely.

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
36
Lessons
152

Frequently Asked Questions

Is the “let, also, and Scope Functions” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “let, also, and Scope Functions”, 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 Android Academy course includes 4 lessons in total.

What will I learn in “let, also, and Scope Functions”?

Work with nullable values. You practise Android 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 Android Academy?

No prior experience is required. Android 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 3 of 4.

How long does the “let, also, and Scope Functions” 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 Android Academy lesson?

Yes. Every Android 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. Nullable vs Non-Null Types
  2. Safe Calls and Elvis
  3. let, also, and Scope Functions
  4. Avoiding NullPointerExceptions
← Back to Android Academy