Javascript String Search Method
This Javascript String Search Method, as the name says, is used to search for some string inside a string. Wait… then what is the difference between this method and the IndexOf method?
Javascript String Search Method
Well, the IndexOf method can take one more argument as the start position, i.e., from which position do you want to start searching the index of some string inside a string? So, you can search in a specific portion of a string using IndexOf, but there is no such argument in the search method. Here is a demonstration of the Javascript String Search Method –
In the above program, you can see that we are searching for some string inside another string using the search method. This Javascript String Search Method is going to return the index of the string in the given string. Now, since we have the string that we are searching for in the given string, let’s have a look at the output now.
Now, If the string you are trying to search is not present in the string, then the search method gives an output of -1.
Let’s have a look at the below program now, which demonstrates the situation when the string is not present in the given string.