JavaScript - check starts with and ends with | test startsWith and endsWith

Add this functions to the String prototype:
if (typeof String.prototype.startsWith != 'function') {
  String.prototype.startsWith = function (str){
    return this.slice(0, str.length) == str;
  };
}
if (typeof String.prototype.endsWith != 'function') {
  String.prototype.endsWith = function (str){
    return this.slice(-str.length) == str;
  };
}
Then use it:
"Starts with JavaScript".startsWith("Starts"); // true
var foo= "Ends with JavaScript";
var bar= "JavaScript";
foo.startsWith(bar); // true