JavaScript | String indexOf()

str.indexOf() function finds the index of the first occurrence of the argument string in the given string. The value returned is 0-based. The syntax of the function is as follows:

str.indexOf(searchValue , index)

Arguments
the first argument to the function searchValue is the string that is to be searched in the base string. The second argument to the function index defines the starting index from where the searchValue is to be searched in the base string.

Return value
This function returns the index of the string (0-based) where the searchValue is found for the first time. If the searchValue cannot be found in the string then the function returns -1.



Examples for the above function are provided below:

Example 1:

print('Departed Train'.indexOf('Train')); 

In this example the function indexOf() finds the index of the string Train. Since the first and the only index where this string is present is 9, therefore this function returns 9 as the answer.

Output:

9

Example 2:

print('Departed Train'.indexOf('ed Tr')); 

Output:

6

In this example the function indexOf() finds the index of the string ed Tr. Since the first and the only index where this string is present is 6, therefore this function returns 6 as the answer.

Example 3:

print('Departed Train'.indexOf('train')); 

Output:

-1

In this example the function indexOf() finds the index of the string Train. Since the searchValue is not present in the string, therefore this function returns -1 as the answer.


Example 4:

print('Departed Train before another Train'.indexOf('Train')); 

Output:

9

In this example the function indexOf() finds the index of the string Train. Since the first index of the searchValue is 9, therefore this function returns 9 as the answer.

Code for the above function are provided below:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate indexOf() function
<script>
function func() {
  
    // Original string
    var str = 'Departed Train';
  
    // Finding index of occurrence of 'Train'
    var index = str.indexOf('Train');
    document.write(index); 
}
func();
</script>

chevron_right


Output:

9

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<script>
// JavaScript to illustrate indexOf() function
function func() {
  
    // Original string
    var str = 'Departed Train';
  
    // Finding index of occurrence of 'Train'
    var index = str.indexOf('ed Tr');
    document.write(index);  
}
func();
</script>  

chevron_right


Output:

6

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate indexOf() function
<script>
function func() {
  
    // Original string
    var str = 'Departed Train';
  
    // Finding index of occurrence of 'Train'
    var index = str.indexOf('train');
    document.write(index);  
}
func();
</script>  

chevron_right


Output:

-1

Program 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

// JavaScript to illustrate indexOf() function
<script>
function func() {
  
    // Original string
    var str = 'Departed Train before another Train';
  
    // Finding index of occurrence of 'Train'
    var index = str.indexOf('Train');
    document.write(index);  
}
func();
</script>  

chevron_right


Output:

9


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.