Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

TypeScript | String indexOf() Method

  • Last Updated : 18 Jun, 2020

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 . 

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • searchValue: This parameter is a string representing the value to search for.
  • fromIndex: This parameter is a location within the calling string to start the search from.

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: 



JavaScript




<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: 

JavaScript




<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
My Personal Notes arrow_drop_up
Recommended Articles
Page :