JavaScript Glossary: String startsWith()

Publikováno: 27.3.2019

Basics

The startsWith method takes a string parameter and checks if the calling string starts with the value provided. It returns a Boolean value, true

Celý článek

Basics

The startsWith method takes a string parameter and checks if the calling string starts with the value provided. It returns a Boolean value, true if the string starts with the provided value and false if not. It is case sensitive meaning the case of the search term needs to match that of the calling string.

"The lazy brown fox just couldn't move".startsWith("The")
// true

https://scotch.io/embed/gist/6a5ef664471ececc67fed3182f4c404d

This method:

  • Takes a string value.
  • Checks if the calling string object begins with the provided parameter.
  • Returns true or false

Syntax

const search = string.startsWith(searchTerm, startIndex)

2 Parameters

searchTerm The term to be searched for at the start of the string.

startIndex The start index to begin searching for the searchTerm. It defaults to 0

Returns a boolean

It returns true if the string starts with the searchTerm and false if otherwise.

Common Uses and Snippets

Checking if a initial contents of a string matches a specific string.

https://scotch.io/embed/gist/19b059573609150a48e982de001cf879

Nahoru
Tento web používá k poskytování služeb a analýze návštěvnosti soubory cookie. Používáním tohoto webu s tímto souhlasíte. Další informace