Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript String indexOf() Method

Improve Article
Save Article
Like Article
  • Last Updated : 04 Jan, 2023
Improve Article
Save Article
Like Article

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

Syntax:

str.indexOf(searchValue , index)

Parameters: 

  • searchValue: The searchValue is the string that is to be searched in the base string. 
  • index: The index defines the starting index from where the search value is to be searched in the base string.

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

Example: Below is an example of the String indexOf() Method. 

JavaScript




<script>
    function func() {
      
        // Original string
        var str = 'Departed Train';
      
        // Finding index of occurrence of 'Train'
        var index = str.indexOf('Train');
        console.log(index); 
    }
    func();
</script>


Output:

9

Examples of the above function are provided below:

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

JavaScript




<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');
        console.log(index);
    }
    func();
</script>


Output: 

6

Example 2: 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.

JavaScript




<script>
    function func() {
      
        // Original string
        var str = 'Departed Train';
      
        // Finding index of occurrence of 'Train'
        var index = str.indexOf('train');
        console.log(index);  
    }
    func();
</script>


Output: 

-1

Example 3: 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.

JavaScript




<script>
    function func() {
      
        // Original string
        var str = 'Departed Train before another Train';
      
        // Finding index of occurrence of 'Train'
        var index = str.indexOf('Train');
        console.log(index);
    }
    func();
</script>


Output: 

9 

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

Supported browser:

  • Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 3 and above
  • Opera 3 and above
  • Safari 1 and above

My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!