Skip to content

ElementAtOrDefault

Dennis Corvers edited this page Oct 23, 2026 · 1 revision

Enumerable:elementAtOrDefault

Returns the value at a specified 1-based position, or a default when that position is not present.

Syntax

value = values:elementAtOrDefault(index, defaultValue)

Parameters

Parameter Type Description
index positive integer 1-based position of the requested value.
defaultValue any (optional) Value returned when the position is outside the iterable.

Returns

The value at index, or defaultValue if there is no value there.

Remarks

The index must still be a positive integer, even when a default is provided. The default is nil if omitted.

Example

local standby = LuaQ.from({
    { name = "Ada" },
    { name = "Bryn" },
})
    :elementAtOrDefault(3, { name = "Reserve" })
-- standby.name is "Reserve"

See also

ElementAt · FirstOrDefault · Functions

Clone this wiki locally