Skip to content

before

The before function returns all elements of an array or characters of a string before the specified index.

Syntax

typescript
before(string: string, index: number): string;
before<T>(array: T[], index: number): T[];

Parameters

NameTypeDescription
arrayT[] | stringSource array or string
indexnumberIndex before which elements are returned

Returns

TypeDescription
T[] | stringElements or characters before the index

Examples

typescript
before([1, 2, 3, 4], 2); // => [1, 2]
before("abcdef", 3); // => "abc"

Notes

  • The index is zero-based.
  • Returns an empty array or string if the index is 0.

References

Released under the MIT License.