JavaScript - How to Check if a String "StartsWith" Another String in JavaScript? Last Updated : 03 Dec, 2024 Comments Improve Suggest changes Like Article Like Report Here are the various methods to check if a string starts with another string in JavaScript1. Using String.startsWith() MethodThe 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() MethodIf 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() MethodAnother 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() MethodYou 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?MethodWhen to UseWhy Choose ItstartsWith()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. Comment More infoAdvertise with us Next Article JavaScript - How to Check if a String "StartsWith" Another String in JavaScript? S sunillaksh Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More 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 JavaScript - How To Count String Occurrence in String? Here are the various methods to count string occurrence in a string using JavaScript.1. Using match() Method (Common Approach)The match() method is a simple and effective way to count occurrences using a regular expression. It returns an array of all matches, and the length of the array gives the co 3 min read How to check a string data type is present in array using JavaScript ? In JavaScript, an array is a collection of data that can be of the same or different type. If we have the array containing the data, our task is to identify if it is a string data type. In this article, we will learn how to use the typeof operator. Syntax: typeof value Note: The typeof operator retu 3 min read How to check a string is entirely made up of the same substring in JavaScript ? We are given a string and the task is to determine whether the string is made up of the same substrings or not. 1. Using Regular Expression with test() method Initialize a string to the variable.Use the test() method to test a pattern in a string.The test() method returns true if the match is found, 3 min read How to check whether a given string is an absolute URL or not in JavaScript? We will learn how to return true if the given string is an absolute URL in JavaScript. There are two types of URLs either relative or absolute.Absolute URL: It is a URL that contains all the information that is important for locating the resources. The thing that is included in the absolute URL is t 3 min read JavaScript - How To Check Whether a String Contains a Substring? Here are the different methods to check whether a string contains a substring in JavaScript.1. Using includes() MethodThe includes() method is the most simple and modern way to check if a string contains a specific substring. It returns true if the substring is found within the string, and false oth 3 min read Like