Skip to content

Instantly share code, notes, and snippets.

@hamiltondanielb
Last active December 5, 2022 12:52
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hamiltondanielb/a174bc415fdd71966de1 to your computer and use it in GitHub Desktop.
Save hamiltondanielb/a174bc415fdd71966de1 to your computer and use it in GitHub Desktop.
ES6 String contains, starts with, ends with to ES5
String.prototype.contains = String.prototype.contains || function(str) {
return this.indexOf(str) >= 0;
};
String.prototype.startsWith = String.prototype.startsWith || function(prefix) {
return this.indexOf(prefix) === 0;
};
String.prototype.endsWith = String.prototype.endsWith || function(suffix) {
return this.indexOf(suffix, this.length - suffix.length) >= 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment