Skip to content

startsWith

The startsWith() method checks whether a string starts with a specific substring.

Interactive example

Syntax

Classic signature

typescript
function startsWith<
	GenericString extends string, 
	GenericSearchString extends string
>(
	input: GenericInput, 
	searchString: GenericSearchString
): input is Extract<GenericInput, `${GenericSearchString}${string}`>

Curried signature

typescript
function startsWith<
	GenericInput extends string,
	GenericSearchString extends string
>(
	searchString: GenericSearchString
): (
	input: GenericInput
) => input is Extract<GenericInput, `${GenericSearchString}${string}`>;

Parameters

  • input: The string to check.
  • searchString: The substring to look for at the start of input.

Return value

Returns true if input starts with searchString and false otherwise.

See also

  • includes: Checks whether a string contains a substring.
  • indexOf: Returns the index of the first occurrence of a substring.
  • lastIndexOf: Returns the index of the last occurrence of a substring.
  • search: Searches for a match with a regular expression.

Sources

Released under the MIT license.