JS - Polyfills - .startsWith()
by Zacc206
JavaScript
console.clear();
// Test to see if browser already supports .startsWith()
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(str, pos) {
pos = pos || 0;
return this.indexOf(str, position) === pos;
};
}
// Create a new str to test
var myStr = 'abracadabra';
// Log results of different tests
console.log('myStr starts with b? ' + myStr.startsWith('b'));
console.log('myStr starts with c? ' + myStr.startsWith('c'));
console.log('myStr starts with a? ' + myStr.startsWith('a'));