In JavaScript programs, people often face the dilemma of whether a JavaScript string contains a substring or not. If you are writing or working with any JavaScript program, it is quite common to get have a substring within a string. If the JavaScript contains string, then there might be a substring also within that string. In this article, we will give you information about how to find a substring within a JavaScript string with the help of two methods.
String contains substring JavaScript – What Is A Substring In JavaScript?
A substring is basically a part of a string that resides inside a string type on a JavaScript program. Getting substrings within a string is quite common in JavaScript programs. To find out substring within a string in your JavaScript program, you will need to check whether a word of string type in your program constitutes any specific character or any specific character set. This way, if the JavaScript string contains character, you will be able to find out the substring within the string.
Javascript String Contains A Substring: How To Find Out?
There are mostly two common ways of finding a substring within a JavaScript string, or whether a JavaScript array contains a substring, in a JavaScript program. In the current section of this article, we will provide you with two different ways of checking whether your JavaScript string contains a substring or not.
For the purpose of finding a solution to this problem, we will make use of two different in-built JavaScript methods. These are:
- The includes () method.
- The indexOf () method.
In the next section, we will take a look at the aforementioned built-in methods of JavaScript. Furthermore, we will also look at how to make use of these methods, in each case, to find whether a JavaScript string contains a substring.
The includes () Method In JavaScript: Applications And Uses
This is the most common method that is used to check whether a JavaScript string contains a specific character or whether it contains a few series of character types.
Here, is a syntax of how the includes () method is generally used in a JavaScript program:
…string.includes (substring, index);… |
Here the codes in the syntax are meant this way:
- string: you are using it to do a search on the JavaScript program.
- includes (): you are using string to call this method to perform the search.
- In this case, the includes () method accepts two different arguments – a required argument and an optional argument.
- The first argument here is checking the substring within the string in the JavaScript program, while the second argument gives the position of the substring in the program, which is why it is considered optional.
Check Whether JavaScript String Contains A Substring Using The includes () Method
let string= “My, World”;let substring = “My”; console.log(string.includes(substring)); ————————-Output: True |
In this JavaScript program, the value returned was “True”. This means that the “My” substring was actually present in the variable “string” in this JavaScript program.
If you change the case or spelling in the string of the JavaScript string type, then the output that will be returned is false. Hence, you will need to keep in mind while you are working with the includes () method that the method is case-sensitive.
The indexOf () Method In JavaScript: Applications And Uses
The indexOf () method is quite similar to the includes () method. This method also checks whether a JavaScript string contains a substring. The following is a general syntax of JavaScript with the indexOf () method:
…string.indexOf(substring, index);… |
Here the codes in the syntax are meant this way:
- string: you are using it to do a search on the JavaScript program.
- indexOf (): you are using string to call this method to perform the search.
- In this case, like the includes () method, the indexOf method is also accepting two different arguments in this JavaScript– a required argument and an optional argument.
- The first argument in the indexOf () method here is checking the substring within the string in the JavaScript program, while the second argument, that is, “index” gives the position of the substring in the program, which is why it is considered optional.
However, the major difference between the two JavaScript methods, in this case, is the variety in the return value of the two different methods. Here, the includes () methods returns a Boolean value of type “True” or “False” in the JavaScript program output, while the indexOf () method returns a number.
Suppose the method indexOf () could not find the string in the JavaScript program. Then the return value, in this case, will be “-1”
Checking Whether JavaScript String Contains A Substring Using The indexOf () Method
let string= “My, World”;let substring = “M”; console.log(string.indexOf(substring)); ————————-Output: 0 |
In this JavaScript program, the value returned was “0”. This means that the “My” substring was not present in the variable “string” in this JavaScript program.
If you change the case or spelling in the string of the JavaScript string type, then the output that will be returned is false. Hence, you will need to keep in mind while you are working with the includes () method that the method is case-sensitive.
Furthermore, if you change the value of the substring from “M” to “my,” the output will then be, in this JavaScript program, will be “-1”.
Summing Up
With the indexOf () and includes () method in your JavaScript program, you can check whether your JavaScript string contains a substring or not. If your JavaScript string contains a substring, the includes () method will give you a Boolean value output of type “true”. Otherwise, it will return “false.” On the other hand, with the help of the indexOf () method, you can check whether your JavaScript string contains a substring in the same way. If the string contains a substring, the program will return a number, that is, “1”. If the JavaScript program string does not contain the substring, then the output will be “0.” Furthermore, if you change the case of the string, the indexOf () method will give -1.
Have A Look :-