Skip to content

first

A função first retorna o primeiro elemento de um array ou o primeiro caractere de uma string.

Sintaxe

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

Parâmetros

NomeTipoDescrição
arrayT[] | stringArray ou string de origem

Retorno

TipoDescrição
T | string | undefinedPrimeiro elemento ou caractere, ou undefined se vazio

Exemplos

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

Notas

  • Retorna undefined se o array ou string estiver vazio.
  • Útil para acessar rapidamente o primeiro item.

Referências

Released under the MIT License.