Web developers may need to check if a string contains substring in JavaScript . This is required if you are providing some kind of search functionality on your site’s web pages. There are several simple ways to do this using JavaScript. In this article, we will learn how to check whether given string contains a substring or not.
Check Whether String Contains Substring
Let us say you have the following string.
var a = 'good morning';
Here are the different ways to check if string contains substring in JavaScript.
1. Using includes()
You can use includes() function to check if a string contains a substring or not. It returns true if the substring is present in the string, else it returns false. It is commonly used by JavaScript for this purpose. Here is its syntax.
string.incudes(substring,[position])
The includes function can be called on any string variable or literal. The first argument is the substring you need to search. The second argument is optional and means the starting point where you want to start the search from.
Here is an example to check if substring ‘good’ is present in our string.
a.includes('good'); // returns true
Please note, includes performs a case-sensitive search so if the substring is not in the proper case, then it will return false.
a.includes('Good'); // returns false
So you can convert the original string to lower/uppercase to match the case of your substring.
console.log(a.toLowerCase().includes('Good'.toLowerCase())) // returns
In the above example, the original string and substring have different cases. So we convert both of them to lowercase before calling includes function. Please note, toLowerCase() does not alter the original string but only its copy used for checking.
If your original string is very large and you have a vague idea about the location of substring, then you can specify the position argument that tells includes function where to start looking. But ensure that you do not start looking after the substring’s position.
a.includes('good',2); // returns false
In the above example, we have specified position as 2, whereas the substring starts from 1st letter so the result is false.
2. Using indexOf()
The indexOf() function returns the position of first occurrence of character or substring in a string. If it is not present then the function will return -1. You can call indexOf() on all string variables and literals. Here is an example to check if substring ‘good’ is present in our substring.
a.indexOf('good'); // returns 0
If you want the result to be boolean, then you can check if the result of indexOf() is >=0.
console.log(a.indexOf('good')>=0); // returns true
Like includes() function, indexOf() is also case sensitive. So it is better to convert both original string and substring to same case before calling indexOf(), as shown in previous solution. It also accepts an optional argument that specifies the start of search. The default position is start of the string 0.
console.log(a.indexOf('good',2)); // returns -1
3. Using search(regExp)
You can also use search function to check if a substring is present in another string. It returns index of 1st match of regular expression in the string. If no match is found, -1 is returned. You can also use literal strings, instead of regular expression. It also performs case insensitive search. Here is a basic example to check if ‘good’ is present in ‘good morning’.
a.search('good'); // returns 0
In the above example, we have used literal string. You can also search using regular expression, enclosed within /…/
a.search(/good/); // returns 0
Here is an example to search for ‘GOOD’ in our original string, using a case insensitive match.
a.search(/GOOD/i); // returns 0
Using regular expressions allows you to search for a wide range of substrings, along with options such as case insensitive search.
4. Using lastIndexOf(searchValue)
The lastIndexOf() function is the opposite of indexOf() function. It returns the position of the last occurrence of substring in a string. If the substring is not found, it returns -1. Here is its syntax.
string.lastIndexOf(substring)
Here is an example to demonstrate it.
var a = 'good morning, good night';
console.log(a.lastIndexOf('good')); // output is 14
In the above example, there are 2 occurrences of ‘good’ in original string. lastIndexOf() function returns 14, that is, the starting position of last occurrence.
You can also specify an optional parameter to specify the starting position of search.
console.log(a.lastIndexOf('good',10)); // output is 0
In the above example, we specify the starting position as 10. Since it starts looking from the 11th character instead of the last character, it skips the 2nd occurrence of ‘good’. So it returns the result as 0, that is, the first occurrence.
5. Using startsWith(searchValue)
If you want to simply check if a substring is present at the beginning of another string, then you can also use startsWith. It returns true the given substring is found at the beginning of a string. Else it returns false. Here is its syntax.
string.startsWith(substring, start)
In the above function, you need to specify the substring to be searched as the first argument. The next argument is optional. It stands for the starting position of the string at which to begin the search. It is useful if you are using a large string, or you are sure about the position of substring.
var a = 'good morning, good night';
console.log(a.startsWith('good')); // returns true
6. Using endsWith(searchValue)
Similarly, JavaScript also provides endsWith() function, that allows you to check if a substring is present at the end of a string or not. Here is its syntax.
string.endsWith(substring, length)
In the above function, we need to specify the substring to be searched as the first argument. Next argument is optional and means the length of the string. By default, it is equal to the length of the string. If your string is long or you know the position of substring, then you may provide a value lesser than the length of string.
Conclusion
In this article, we have learnt several different ways to check if string contains substring in JavaScript. Among them, using includes() function is the simplest way to check for a substring. If you need to search for a substring’s position in addition to checking its presence, then you can use indexOf(). We also looked several JavaScript functions built for specific use cases.
Also read:
How to Remove Empty Elements from JavaScript Array
How to Detect Invalid Date in JavaScript
How to Find Sum of Array of Numbers in JavaScript

Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.