Check for Substring in JavaScript Last Updated : 16 Nov, 2024 Comments Improve Suggest changes Like Article Like Report Given two strings, check if one string is substring of another."bce" is substring of "abcde""ae" is not substring of "abcde"Empty String is a substring of all stringsUsing includes() - Most used and Simplest MethodThe includes() method checks whether a string contains a substring. JavaScript let s = "abcde"; let res = s.includes("bcd"); console.log(res); Outputtrue Using indexOf() - Gives Index of First Occurrence as wellThe indexOf() method returns the index of the first occurrence of a specified substring in a string. If the substring is not found, it returns -1. JavaScript let s = "Hello, world!"; let res = s.indexOf("world") !== -1; console.log(res); Outputtrue Using Regular Expressions (RegExp)Regular expressions can be used to check if a substring exists within a string by using the .test() method. JavaScript let s = "Hello, world!"; let pat = /world/; let res = pat.test(s); console.log(res); Outputtrue In this example, the regular expression /world/ checks for the presence of the word "world" in the string.Which Approach is Better in Different Cases?includes() Method: The simplest and most readable approach. Use it when you need to check if a substring is present.indexOf() Method: Useful if you need the position of the substring, not just a Boolean check.Regular Expressions (RegExp): Use this method for more complex pattern matching, such as case-insensitive checks or partial matches.Check Whether a String Contains a Substring in JavaScript - FAQsIs the includes() method case-sensitive?Yes, includes() is case-sensitive, so "Hello" and "hello" would be considered different.What happens if the substring is not found with indexOf()?If the substring is not found, indexOf() returns -1.Can I perform a case-insensitive search with includes()?No, includes() itself is case-sensitive, but you can convert both strings to the same case using toLowerCase() or toUpperCase().Are regular expressions slower than includes()?Regular expressions can be slower for simple searches, but they are more powerful for complex pattern matching.How do I use includes() with an array of strings?Use Array.prototype.some() in combination with includes() to check if any element in the array contains a specific substring. Comment More infoAdvertise with us Next Article Check for Substring in JavaScript P ProgrammerAnvesh Follow Improve Article Tags : JavaScript Web Technologies javascript-string JavaScript-DSA JavaScript-Questions +1 More Similar Reads Check for Subarray in JavaScript Here are the different approaches to check for Subarray in JavaScriptUsing String Conversion with join() - Best MethodThis approach converts both arrays into strings and checks if the subarray string is contained within the master array string.JavaScriptfunction checkSub(a, sub) { return a.join(',') 2 min read Check if Strings are Equal in JavaScript These are the following ways to check for string equality in JavaScript:1. Using strict equality operator - Mostly UsedThis operator checks for both value and type equality. it can be also called as strictly equal and is recommended to use it mostly instead of double equals.JavaScriptlet s1 = 'abc'; 2 min read JavaScript String Exercise JavaScript string is a primitive data type and is used for storing and manipulating a sequence of characters. It can contain zero or more characters within single or double quotes. This article contains a wide collection of JavaScript Programming examples based on String. JavaScriptlet s = "Geeksfor 7 min read Remove a Character From String in JavaScript In JavaScript, a string is a group of characters. Strings are commonly used to store and manipulate text data in JavaScript programs, and removing certain characters is often needed for tasks like:Removing unwanted symbols or spaces.Keeping only the necessary characters.Formatting the text.Methods t 3 min read JavaScript - Convert a String to Boolean in JS Here are different ways to convert string to boolean in JavaScript.1. Using JavaScript == OperatorThe == operator compares the equality of two operands. If equal then the condition is true otherwise false. Syntaxconsole.log(YOUR_STRING == 'true');JavaScriptlet str1 = "false"; console.log(str1 == 'tr 3 min read Check if a variable is a string using JavaScript Checking if a variable is a string in JavaScript is a common task to ensure that the data type of a variable is what you expect. This is particularly important when handling user inputs or working with dynamic data, where type validation helps prevent errors and ensures reliable code execution.Below 3 min read String in DSA Using JavaScript A string in JavaScript is a sequence of characters enclosed in single ('), double ("), or backticks (`). Strings in JavaScript are immutable, meaning their contents cannot be changed after creation. Any operation that modifies a string actually creates a new string.Example:JavaScriptlet s = "GfG"; c 2 min read JavaScript - Delete Character from JS String In JavaScript, characters can be deleted from the beginning, end, or any specific position in a string. JavaScript provides several methods to perform these operations efficiently.Delete First CharacterTo remove the first character from a string, we can use methods like slice, substring, or regular 2 min read How to Check empty/undefined/null String in JavaScript? Empty strings contain no characters, while null strings have no value assigned. Checking for an empty, undefined, or null string in JavaScript involves verifying if the string is falsy or has a length of zero. Here are different approaches to check a string is empty or not.1. Using === OperatorUsing 2 min read Maximum Frequency Character in String in JavaScript A string is a data structure in JavaScript that is used to store a set of characters. A string can contain a character, a word, or a sentence. A string also can be empty. It is represented using double quotes, single quotes, or template literals. Examples:Input: str = "abcdabac"Output: aExplanation: 3 min read Like