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