Open In App

TypeScript | String indexOf() Method

The indexOf() is an inbuilt function in TypeScript which is used to get the index within the calling String object of the first occurrence of the specified value. 
Syntax:

string.indexOf(searchValue[, fromIndex]) 

Parameter: This method accepts two parameter as mentioned above and described below . 



Return Value: This method returns the index of the found occurrence, otherwise -1 if not found.

Below example illustrate the  String indexOf() method in TypeScriptJS:
Example 1: 






<script>
    // Original strings
    var str1 = new String('TypeScript | \
String indexOf() Method with example'); 
  
    // use of String indexOf() Method
    console.log(str1.indexOf("String"));
</script>

Output: 

13

Example 2: 




<script>
    // Original strings
    var str1 = new String('TypeScript | \
    String indexOf() Method with example'); 
  
    // use of String indexOf() Method
    console.log(str1.indexOf("Geeks"));
</script>

Output: 

-1
Article Tags :