Skip to content

lastOr

ts
function Array.lastOr<T, U>(
    target: readonly T[],
    or: U,
): Exclude<T, null | undefined> | U

Returns the last element of array, or fallback if the array is empty.

Example

ts
import { Array } from "@monstermann/array";

Array.lastOr([1, 2, 3, 4], 0); // 4
ts
import { Array } from "@monstermann/array";

pipe([1, 2, 3, 4], Array.lastOr(0)); // 4