|
Last change
on this file was a762898, checked in by istevanoska <ilinastevanoska@…>, 5 months ago |
|
Added visualizations
|
-
Property mode
set to
100644
|
|
File size:
877 bytes
|
| Line | |
|---|
| 1 | /**
|
|---|
| 2 | * Returns the last element of an array.
|
|---|
| 3 | *
|
|---|
| 4 | * This function takes an array and returns the last element of the array.
|
|---|
| 5 | * If the array is empty, the function returns `undefined`.
|
|---|
| 6 | *
|
|---|
| 7 | * Unlike some implementations, this function is optimized for performance
|
|---|
| 8 | * by directly accessing the last index of the array.
|
|---|
| 9 | *
|
|---|
| 10 | * @template T - The type of elements in the array.
|
|---|
| 11 | * @param {ArrayLike<T> | null | undefined} arr - The array from which to get the last element.
|
|---|
| 12 | * @returns {T | undefined} The last element of the array, or `undefined` if the array is empty.
|
|---|
| 13 | *
|
|---|
| 14 | * @example
|
|---|
| 15 | * const arr = [1, 2, 3];
|
|---|
| 16 | * const lastElement = last(arr);
|
|---|
| 17 | * // lastElement will be 3
|
|---|
| 18 | *
|
|---|
| 19 | * const emptyArr: number[] = [];
|
|---|
| 20 | * const noElement = last(emptyArr);
|
|---|
| 21 | * // noElement will be undefined
|
|---|
| 22 | */
|
|---|
| 23 | declare function last<T>(array: ArrayLike<T> | null | undefined): T | undefined;
|
|---|
| 24 |
|
|---|
| 25 | export { last };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.