Saturday, November 15, 2025
HomeLanguagesJavascriptTypeScript | String lastIndexOf() Method

TypeScript | String lastIndexOf() Method

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

string.lastIndexOf(searchValue[, fromIndex]) 

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

  • 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 last found occurrence, otherwise -1 if not found.

Example 1: 

JavaScript




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


Output: 

17

Example 2: 

JavaScript




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


Output: 

-1
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7143 POSTS0 COMMENTS
Thapelo Manthata
6838 POSTS0 COMMENTS
Umr Jansen
6840 POSTS0 COMMENTS