Skip to content

Instantly share code, notes, and snippets.

@davidwaterston
Last active October 13, 2015 11:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwaterston/4191094 to your computer and use it in GitHub Desktop.
Save davidwaterston/4191094 to your computer and use it in GitHub Desktop.
Javascript: Case-sensitive 'starts with'
if (typeof String.prototype.startsWith != 'function') {
String.prototype.startsWith = function(str) {
return (this.lastIndexOf(str, 0) === 0);
};
}
@davidwaterston
Copy link
Author

A simple Javascript function to tell if one string starts with another.

Usage:

if (myString.startsWith("anything") { do something… }

NOTE: As always, make sure this is defined before it is used!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment