jskatas.org Continuously Learn JavaScript. Your Way.

String API: string.startsWith()

Determines whether a string begins with the characters of another string.

Donate to NGO Julenka. Support Ukranians in need. Julenka is an NGO which my brother founded in 2011 to support Ukranian families and kids in need.

str.startsWith(searchString) determines whether str begins with searchString.

the 1st parameter, the string to search for

  • can be just a character
  • can be a string
  • can contain unicode characters
  • a regular expression throws a TypeError

the 2nd parameter, the position where to start searching from

  • e.g. find "str" at position 4
  • for undefined is the same as 0
  • the parameter gets converted to an int
  • a value larger than the string`s length, returns false

this functionality can be used on non-strings too

  • e.g. a boolean
  • e.g. a number
  • also using the position works

Links

The official specification, actually quite good to read for this function.
The Mozilla Developer Network docs, contains good examples.