xxxxxxxxxx
const str = "Hello, World!";
const prefix = "Hello";
if (str.startsWith(prefix)) {
console.log("String starts with the prefix!");
} else {
console.log("String does not start with the prefix.");
}
xxxxxxxxxx
var str = "Hello world, welcome to the universe.";
var n = str.startsWith("Hello");
xxxxxxxxxx
//checks if a string starts with a word
function startsWith(str, word) {
return str.lastIndexOf(word, 0) === 0;
}
startsWith("Welcome to earth.","Welcome"); //true
xxxxxxxxxx
const str1 = 'Saturday night plans';
console.log(str1.startsWith('Sat'));
// expected output: true
console.log(str1.startsWith('Sat', 3));
// expected output: false
xxxxxxxxxx
JavaScript startsWith() Case sensitive Example
const text = 'Hello, Welcome to JavaScript World';
console.log(text.startsWith('Hello')); // true
console.log(text.startsWith('hello')); // false
xxxxxxxxxx
const s = 'I am going to become a FULL STACK JS Dev with Coderslang';
console.log(s.startsWith('I am')); // true
console.log(s.startsWith('You are')); // false
xxxxxxxxxx
const str = 'Hello, world!';
const substring = 'Hello';
const startsWithSubstring = str.startsWith(substring);
console.log(startsWithSubstring); // Output: true
xxxxxxxxxx
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
value: function(search, rawPos) {
var pos = rawPos > 0 ? rawPos|0 : 0;
return this.substring(pos, pos + search.length) === search;
}
});
}