JavaScript - How to Check if a String "StartsWith" Another String in JavaScript?
Last Updated :
03 Dec, 2024
Here are the various methods to check if a string starts with another string in JavaScript
1. Using String.startsWith() Method
The most widely used method is the startsWith() method, built into JavaScript. This method is simple to use, highly readable, and performs the task efficiently.
JavaScript
let s = 'Hello World';
let pre = 'Hello';
if (s.startsWith(pre)) {
console.log(`The string starts with "${pre}"`);
} else {
console.log(`The string does not start with "${pre}"`);
}
OutputThe string starts with "Hello"
- startsWith() checks if the string begins with the substring prefix.
- It returns true if the condition is met, otherwise false.
- This method is case-sensitive and takes an optional second parameter to specify the position in the string to start the search.
2. Using String.slice() Method
If you're working with older versions of JavaScript or need to perform additional manipulation, slice() can also be used to extract the beginning of a string and compare it to a target substring.
JavaScript
let s = 'Hello World';
let prefix = 'Hello';
if (s.slice(0, prefix.length) === prefix) {
console.log(`The string starts with "${prefix}"`);
} else {
console.log(`The string does not start with "${prefix}"`);
}
OutputThe string starts with "Hello"
- slice(0, prefix.length) extracts the portion of the string from the start (0) to the length of the prefix.
- If the extracted portion matches the prefix, it confirms that the string starts with that substring.
3. Using String.indexOf() Method
Another approach is to use indexOf(), which returns the position of the first occurrence of the specified substring. If the index is 0, it indicates that the string starts with the given substring.
JavaScript
let s = 'Hello World';
let prefix = 'Hello';
if (s.indexOf(prefix) === 0) {
console.log(`The string starts with "${prefix}"`);
} else {
console.log(`The string does not start with "${prefix}"`);
}
- indexOf() returns 0 if the substring is found at the start of the string.
- This method works well but may be less used compared to startsWith().
4. Using String.substr() Method
You can also use the substr() method to extract a portion of the string and compare it to the desired substring. While this method is deprecated in some environments, it still works in most modern browsers.
JavaScript
let s = 'Hello World';
let prefix = 'Hello';
if (s.substr(0, prefix.length) === prefix) {
console.log(`The string starts with "${prefix}"`);
} else {
console.log(`The string does not start with "${prefix}"`);
}
- substr(0, prefix.length) extracts the first prefix.length characters from the string.
- The extracted portion is compared with the prefix to check if the string starts with it.
Which Approach to Choose?
Method | When to Use | Why Choose It |
---|
startsWith() | For modern JavaScript, most cases, and readability. | Simple, efficient, and built-in method for checking prefixes. |
slice() | When working with environments that don't support startsWith(). | Offers flexibility in manipulating and comparing string portions. |
indexOf() | For backward compatibility or when already familiar with the method. | Works well but less intuitive than startsWith() for prefix checks. |
substr() | When dealing with legacy code or old JavaScript versions. | Useful but now deprecated in favor of slice() or substring(). |
The startsWith() method is the simplest and most efficient solution for modern JavaScript. If you're working with older versions of JavaScript or need to manipulate the string in other ways, alternatives like slice(), indexOf(), and substr() are still valid options, though they may require more lines of code and less intuitive logic.
Similar Reads
Javascript Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output
2 min read
How to check a string starts/ends with a specific string in jQuery ? JavaScript provides a lot of string methods that check whether a string is a substring of another string. So, jQuery wouldn't be necessary at all to perform this task. However, we will cover all the different ways of checking whether a string starts or ends with a string:Â startsWith() and endsWith()
5 min read
How to check a string starts/ends with a specific string in PHP ? In this article, we are going to learn how to check that a given string is starting or ending with a given specific string in PHP.We require the use of XAMPP, Netbeans to run this code.Input: "Geeks For Geeks"Starting with: "G"Output: "Geeks For Geeks Starts with String G"In PHP version 8+, there ar
2 min read
How to Search a String for a Pattern in JavaScript ? Here are two ways to search a string for a pattern in JavaScript.1. Using the string search() MethodThe JavaScript string search() method is used to search a specified substring within the given string and regular expression. It returns the index of the first occurrence of the pattern or -1 if the p
2 min read
Check If An Array of Strings Contains a Substring in JavaScript Here are the different ways to check if an array of strings contains a substring in JavaScript1. Using includes() and filter() methodWe will create an array of strings, then use includes() and filter() methods to check whether the array elements contain a sub-string.JavaScriptconst a = ['Geeks', 'gf
4 min read