Array/Since
1.38.0last
Returns the last element of an array.
last(array):T | undefinedParameters
arrayT[]requiredThe array from which to retrieve the last element.
Returns
T | undefinedThe last element of the array, or undefined if the array is empty.
Examples
01
Basic usage
Get the last element from an array of numbers.
TypeScript
import { last } from "@cyref/js";
const numbers = [10, 20, 30, 40];
const result = last(numbers);
console.log(result);
// 4002
With strings
The function works with arrays containing strings.
TypeScript
import { last } from "@cyref/js";
const languages = ["JavaScript", "TypeScript", "Python"];
const result = last(languages);
console.log(result);
// "Python"03
Empty array
An empty array returns undefined.
TypeScript
import { last } from "@cyref/js";
const result = last([]);
console.log(result);
// undefinedBuilt with@cyref/js·Official documentation