When writing code, it is quite common to test patterns against string ends. For a long time, JavaScript did not provide proper support for this use
case. As a result, developers have been relying on various programming subtleties to check the start or end of a string. Examples are getting the
index of a substring, slicing the beginning of a string, extracting a substring from the head, matching a regular expression beginning or ending with
a pattern, and so on.
While these approaches are all technically valid, they look more like hacking than anything else, blur the developer’s intent, but more importantly
affect code readability.
Since ES2015, JavaScript provides String#startsWith
and String#endsWith
, which are the preferred ways to test patterns
against string ends.