VDL Plugin SDK

last

Import from @varavel/vdl-plugin-sdk/utils/arrays.

Utility functions re-exported from es-toolkit (MIT License). See https://github.com/toss/es-toolkit for more details.

Call Signature

function last<T>(arr): T;

Returns the last element of an array.

This function takes an array and returns the last element of the array. If the array is empty, the function returns undefined.

Unlike some implementations, this function is optimized for performance by directly accessing the last index of the array.

Type Parameters

Type ParameterDescription
TThe type of elements in the array.

Parameters

ParameterTypeDescription
arrreadonly [T, T]The array from which to get the last element.

Returns

T

The last element of the array, or undefined if the array is empty.

Example

const arr = [1, 2, 3];
const lastElement = last(arr);
// lastElement will be 3

const emptyArr: number[] = [];
const noElement = last(emptyArr);
// noElement will be undefined

Call Signature

function last<T>(arr): T | undefined;

Returns the last element of an array.

This function takes an array and returns the last element of the array. If the array is empty, the function returns undefined.

Unlike some implementations, this function is optimized for performance by directly accessing the last index of the array.

Type Parameters

Type ParameterDescription
TThe type of elements in the array.

Parameters

ParameterTypeDescription
arrreadonly T[]The array from which to get the last element.

Returns

T | undefined

The last element of the array, or undefined if the array is empty.

Example

const arr = [1, 2, 3];
const lastElement = last(arr);
// lastElement will be 3

const emptyArr: number[] = [];
const noElement = last(emptyArr);
// noElement will be undefined