In this example, the indexOf() method returned 6 which is the position of 'The' in the string 'TechOnTheNet'.
Next, the indexOf() method can search for multiple characters in a string.
Specifying Multiple Characters as the Substring So in this case, the substring 'T' is found at position 6 in the string 'TechOnTheNet'. This means that the search will begin looking for the value 'T' starting at position 4 in the string. In this example, we have set the start_position parameter to a value of 4. The following will be output to the web browser console log: 6 You can change the position where the search will start in the string by providing a start_position parameter to the indexOf() method. In this example, the indexOf() method returned 11 because the first occurrence of 't' within 'TechOnTheNet' is position 11 in the string. The following will be output to the web browser console log: 11 We have written the output of the indexOf() method to the web browser console log, for demonstration purposes, to show what the indexOf() method returns. We have then invoked the indexOf() method of the totn_string variable to look for a substring within totn_string. In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'.
Let's take a look at an example of how to use the indexOf() method in JavaScript.įor example: var totn_string = 'TechOnTheNet'