Skip to content

times

Executes a callback function a specific number of times, returning an array with the results.

Syntax

typescript
times<T>(callback: (index: number) => T, count: number): T[]

Parameters

NameTypeDescription
callback(index: number) => TFunction to be executed on each iteration, receiving the current index.
countnumberNumber of times the function will be executed (must be a non-negative integer).

Returns

TypeDescription
T[]Array containing the results of each callback execution.

Example

typescript
times(i => i * 2, 4); // [0, 2, 4, 6]

Notes

  • Throws a TypeError if the callback is not a function or if count is not a non-negative integer.
  • Useful for generating arrays of values or performing controlled side effects.

References

Released under the MIT License.