Skip to content

first ​

The first function returns the first element of an array or the first character of a string.

Syntax ​

typescript
first<T>(array: T[]): T | undefined;
first(string: string): string | undefined;

Parameters ​

NameTypeDescription
arrayT[] | stringSource array or string

Returns ​

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

Examples ​

typescript
first([1, 2, 3]); // => 1
first("abc"); // => "a"
first([]); // => undefined

Notes ​

  • Returns undefined if the array or string is empty.
  • Useful for quickly accessing the first item.

References ​

Released under the MIT License.