Skip to content

last

The last function returns the last element(s) of an array or character(s) of a string.

Syntax

typescript
last(string: string, count?: number): string;
last<T>(array: T[], count?: number): T[];

Parameters

NameTypeDescription
arrayT[] | stringSource array or string
countnumberNumber of elements/characters to return (optional, default 1)

Returns

TypeDescription
T | string | undefinedLast element or character, or undefined if empty

Examples

typescript
last([1, 2, 3, 4], 2); // => [3, 4]
last('abcdef', 3); // => 'def'
last([1, 2, 3]); // => [3]

Notes

  • If count is greater than the length of the array/string, returns all elements.
  • Returns an empty array or string if the array/string is empty.

References

Released under the MIT License.