> For the complete documentation index, see [llms.txt](https://gold.gitbook.io/kotlin/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gold.gitbook.io/kotlin/collections/elements-operations/elementatorelse.md).

# elementAtOrElse

```kotlin
public inline fun <T> List<T>.elementAtOrElse(index: Int, defaultValue: 
(Int)-> T): T {
    return if (index >= 0 && index <= lastIndex) get(index) else defaultValue(index)
}


val list = listOf(1, 2, 3, 4, 5)
println(list.elementAtOrElse(2, {it * 2}))  // 3
println(list.elementAtOrElse(10, {it * 2}))  // 20
```
