indexOf
Join the Discussion
The indexOf method is the simplest way to determine if a text string contains a particular character or group of characters. The simplest way to describe how it works is to look at an example.
var toFind = 'wheel';
alert(mystring.indexOf(toFind));
In this example mystring is the text string that we want to test and toFind is the text that we want to search for. The alert box will tell us what the indexOf method returns as a result of the search (4 in this example).
The method actually returns the position where the string to find starts in the string being searched (the first position is 0 so wheel starts in position 4). If the string can't be found at all then -1 is returned making it easy to test if the text is found using:
indexOf locates the first occurrence of the text within the string, if you need the last occurrence instead then you can use lastIndexOf in exactly the same way.

