Skip to content

repeat

A função repeat cria um novo array ou string repetindo o valor informado um número específico de vezes.

Sintaxe

typescript
repeat<T>(value: T, count: number): T[];
repeat(value: string, count: number): string;

Parâmetros

NomeTipoDescrição
valueT | stringValor ou string a ser repetido
countnumberNúmero de repetições

Retorno

TipoDescrição
T[] | stringNovo array ou string repetido

Exemplos

typescript
repeat(1, 3); // => [1, 1, 1]
repeat('a', 4); // => 'aaaa'
repeat([2], 2); // => [[2], [2]]

Notas

  • Se count for 0, retorna um array ou string vazio.
  • Lança erro se count for negativo.

Referências

Released under the MIT License.