Skip to content

slice

The slice() method extracts a section of a string and returns a new string without modifying the original string.

Interactive example

Syntax

Classic signature

typescript
function slice<
	GenericInput extends string
>(
	input: GenericInput,
	start: number,
	end: number
): string

Curried signature

typescript
function slice<
	GenericInput extends string
>(
	start: number,
	end: number
): (input: GenericInput) => string

Parameters

  • input: The string to extract from.
  • start: The start index of the extraction (inclusive). If negative, counts from the end.
  • end: The end index of the extraction (exclusive). If negative, counts from the end.

Return value

A new string containing the section extracted from the original string.

See also

  • substring - Extracts a substring between two indexes
  • charAt - Returns the character at an index
  • at - Returns the character at an index with negative index support

Sources

Released under the MIT license.