jskatas.org Continuously Learn JavaScript. Your Way.

String API: string.endsWith()

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.endsWith(searchString) determines whether str ends with searchString

the 1st parameter the string to search for

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

the 2nd parameter, the position where the search ends (as if the string was only that long)

  • find "el" at a substring of the length 2
  • undefined uses the entire string
  • the parameter gets coerced to an integer (e.g. "5" becomes 5)

value less than 0

  • returns true, when searching for an empty string
  • return false, when searching for a non-empty string

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 some examples.