Thursday, September 4, 2025
HomeLanguagesJavascriptTypeScript | Array indexOf() Method

TypeScript | Array indexOf() Method

The Array.indexOf() is an inbuilt TypeScript function which is used to find the index of the first occurrence of the search element provided as the argument to the function. 
 Syntax:

array.indexOf(searchElement[, fromIndex])

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

  • searchElement : This parameter is the Element to locate in the array.
  • fromIndex : This parameter is the index at which to begin the search.

Return Value: This method returns the index of the found element. 
Below examples illustrate the Array indexOf() method in TypeScript.

Example 1: 

JavaScript




<script>
    // Driver code
    var arr = [ 11, 89, 23, 7, 98 ]; 
  
    // use of indexOf() method 
    var val = arr.indexOf(7)
       
    // printing element
    console.log(val);
</script>


Output: 

3

Example 2: 

JavaScript




<script>
    // Driver code
    var arr = [ 'a','b','c','a','e' ]; 
  
    // use of indexOf() method 
    var val = arr.indexOf('a',4)
       
    // printing element
    console.log(val);
</script>


Output: 

-1
RELATED ARTICLES

Most Popular

Dominic
32263 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6627 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6696 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS