The 'also' function in Kotlin is useful for executing additional side effects on an object while keeping the object itself unchanged. This is particularly handy when you want to perform operations such as logging or modifying properties without altering the original object. It enhances code readability by clearly indicating the intention of performing a side effect.
data class User(val name: String, var age: Int)
fun main() {
val user = User("Alice", 25)
user.also {
println("User before update: $it")
it.age += 1 // Increase age by 1
}.also {
println("User after update: $it")
}
}