🐦 Kotlin
Beginner
What is the Elvis operator (?:) in Kotlin?
Answer
The Elvis operator ?: provides a default value when the left-hand expression is null. The expression name?.length ?: 0 returns the length of name if it is not null, or 0 if it is null. The name comes from the Elvis emoji (the ?: emoticon looks like Elvis's hair and eye). You can also throw an exception as the right-hand side: val name = user?.name ?: throw IllegalArgumentException("User must have a name"). This is extremely useful for providing fallback values concisely.